database concepts · 2020-01-14 · database concepts page 2 of 19 i. alternate key: out of the...
TRANSCRIPT
Database Concepts
Page 1 of 19
Q1. What is a Database?
Ans: A Database is an organized collection of related information that supports for easy access, modification and maintenance.
Q2. What is a DBMS?.
Ans: Database is managed by special software packages known as Database Management Systems (DBMSs). The purpose of DBMSs software is to allow the user to create, modify and
administration of database.
Q3. What is a RDBMS? Name some popular RDBMS
Ans: In the Relational data model, database is represented as a collection of related Tables. Database management software used to manage Relational Databases is called a Relational Data Base
Management System. Examples of Relational database management systems are: MS-Access, MySQL, PostgreSQL, SQLite, Microsoft SQL Server, Oracle, SAP, dBase, FoxPro, etc.
Q4. What are the benefits of using a DBMS?
Ans: Benefits of DBMS are:
a. Redundancy (duplicity) can be controlled b. Inconsistency can be avoided
c. Data can be shared d. Security restrictions can be applied.
Q5. Explain the following terms
A. Relation: A relation means a 'Table', in which data is organized in the form of rows and columns. Relation: Student
ADNO NAME CLASS SECTION AVERAGE
101 Anu 12 A 85
105 Balu 12 D 65
203 Leena 11 B 95
205 Madhu 10 B 70
305 Surpreeth 9 C 70
483 Usha 6 A 60
B. Tuple/Record/Row:
Horizontal subset of information in a table is called Tuple. Example:
101 Anu 12 A 85
C. Attribute /Field/Column/Key:
Vertical subset of information in a table is called Attribute. Example: ADNO , NAME , CLASS , SECTION , AVERAGE
D. Domain: A domain is defined as the set of all unique values permitted for a column. A domain
of date column is the set of valid dates, a domain of integer column is whole numbers, a domain of SECTION is ('A','B','C','D')
E. Cardinality: Cardinality is number of rows (tuples) in a table.
Example: Cardinality of Relation Student is 6 (Number of rows).
F. Degree: The degree is the number of attributes (columns) in a table.
Example: Degree of Relation Student is 5 (Number of columns).
G. Candidate Key: Candidate keys are all those columns in a relation which have unique values and hence are eligible to act as a primary key. Example: In the above table, AdNo and Name
both have unique values. Therefore, both AdNo and Name are candidate keys.
H. Primary Key: A column or set of columns that uniquely identify a row within a table is called its
primary key. Example: Out of the candidate keys: AdNo and Name, only AdNo is selected as the primary key.
Database Concepts
Page 2 of 19
I. Alternate Key: Out of the candidate keys, after selecting one primary key, the remaining
candidate keys which are not made primary key are called alternate key. Example: From the candidate keys, AdNo is selected as the primary key, therefore the remaining candidate key is
Name and it is the Alternate key.
J. Foreign Key: A foreign key is a column in one table that uniquely identifies a row of another table. In other words, a foreign key is a column or a combination of columns that is used to
establish a link between two tables.
Q6. What is SQL? What is its use?
Ans: Structured Query Language (SQL) is a standard language used for accessing databases. This is a special purpose programming language used to create a table, manage data and manipulate
data.
Q7. What are the advantages of SQL? 1) SQL is portable running in all servers, mainframes, PCs, laptops, and even mobile phones.
2) High speed SQL queries can retrieve large amounts of information quickly and efficiently.
3) SQL is easy to learn and understand. 4) SQL is used with Relational databases with any vendor to create, manage and secure
databases. 6) SQL acts as both Programming language and Interactive language.
7) It provides Client Server Architecture. 8) Supports Object Based Programming and is highly flexible.
Q8. What are the different types of SQL statements?
1. Data Definition Language (DDL) statements is a set of commands that defines the different
structures in a database. DDL statements are used to create databases and table structures, modify the existing structure of the table and remove the existing table. Some of the DDL
statements are CREATE TABLE, ALTER TABLE and DROP TABLE.
2. Data Manipulation Language (DML) statements are used to access and manipulate data in existing tables. The manipulation includes inserting data into tables, deleting data from the
tables, retrieving data and modifying the existing data. The common DML statements are SELECT, UPDATE, DELETE and INSERT.
3. Data Control Language (DCL): This is a category of SQL commands which are used to control the access to databases and tables. Examples of DCL commands are - GRANT, REVOKE.
MySQL Commands
1. CREATE DATABASE: (DDL) It creates a database with the specified name. CREATE DATABASE databasename;
2. SHOW DATABASES: (DML) It shows a list of databases present in the system.
SHOW DATABASES;
3. USE: (DML) It opens the specified database for use.
USE databasename;
4. SELECT DATABASE() :It Shows the name of the current database (DML) SELECT DATABASE();
5. SHOW TABLES: (DML) It shows a list of tables present in the current database.
SHOW TABLES;
Ques: What are the different datatypes available in MySQL? What is their purpose?
1. CHAR: CHAR is used for storing fix length character strings. String values will be right padded
with spaces to bring it up to some fixed size before they are stored on the disk. Thus it wastes a lot of disk space.
Format: CHAR(n)
Database Concepts
Page 3 of 19
2. VARCHAR: Varchar is used for variable character string. It can have maximum of 2000
characters. Format: VARCHAR (n)
3. DECIMAL or NUMERIC: Decimal and numeric data types have fixed precision and scale.
Format: DECIMAL (p , s) and NUMERIC (p , s) 'p' is the precision or the total number of significant places including one place for decimal point.
's' is the scale or the number of digits after the decimal point.
Example, a column defined with DECIMAL(6,2) would allow the value: 1 2 3 . 4 5 (p=6, s=2) and DECIMAL(5,2) would allow the value: 1 2 . 3 4 (p=5,s=2)
4. INT/INTEGER: The INT data type is the integer data type in SQL. This is used to store whole
number (without any fraction part).
5. DATE is used to store valid date values, which is ranging from January 1, 4712 BC to December 31, 9999 AD. The date values are in the format YYYY-MM-DD
Format: DATE
Ques: What are the different Constraints available in MySQL? What is their purpose?
MySQL provides some rules, called Constraints, which help us to ensure validity of the
data. These constraints are:
PRIMARY KEY : Sets a column or a group of columns as the Primary Key of a table.
Therefore, NULLs and Duplicate values in this column are not accepted.
NOT NULL : Makes sure that NULLs are not accepted in the specified column.
6. CREATE TABLE: (DDL) It creates a table with the specified name. We need to specify the
column names, data types, size and constraints for each column. Table and Column names
cannot start with a digit but can contain both letters and numbers. Spaces and symbols are not
allowed except underscore (_). CREATE TABLE tablename
( columnname1 datatype(size) constraint,
columnname2 datatype(size) constraint, ….
ColumnnameN datatype(size) constraint );
Note: Do not put a comma after last column information
Ques Create a table student with the following structure:
Fields Data Type Constraints admno numeric(5) PRIMARY KEY name varchar(20) NOT NULL class integer(2) section char(1) dob date fees numeric(7,2) create table student ( admno numeric(5) primary key, name varchar(20) not null, class integer(2), section char(1), dob date, fees numeric(7,2) );
Database Concepts
Page 4 of 19
7. DESCRIBE : (DML) It shows the table structure as well as constraints, if any.
DESCRIBE tablename;
Example: desc student;
+---------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+--------------+------+-----+---------+-------+ | admno | decimal(5,0) | NO | PRI | NULL | | | name | varchar(20) | NO | | NULL | | | class | integer(2) | YES | | NULL | | | section | char(1) | YES | | NULL | | | dob | date | YES | | NULL | | | fees | decimal(7,2) | YES | | NULL | | +---------+--------------+------+-----+---------+-------+
8. INSERT : (DML) It inserts a data Row into the table
INSERT INTO tablename (column1, column2, ..., columnN) VALUES (value1, value2, ...valueN);
We can insert only one Row at a time. Char, Varchar and Date values are enclosed in single
quotes ' ' . Numbers are written normally. Dates are specified in the format 'YYYY-MM-DD'. To insert the current date into a table, MySQL's built-in function CURDATE() can be used.
Ques Insert the following records in the table student:
+-------+--------------+-------+---------+------------+---------+ | admno | name | class | section | dob | fees | +-------+--------------+-------+---------+------------+---------+ | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | | 222 | Mohit Sharma | 11 | B | 2001-12-31 | 4500.00 | | 333 | K.P. Gupta | 12 | B | 2000-01-11 | 3000.00 | | 444 | Ajit Kumar | 10 | A | 2002-03-04 | 2000.00 | | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | | 666 | Rohan Sharma | 11 | B | 2001-12-05 | 2500.00 | | 777 | Leena | NULL | NULL | 2000-03-05 | NULL | +-------+--------------+-------+---------+------------+---------+ insert into student values (111, 'Anu Jain', 12, 'A', '2000-12-31',2500); insert into student values (222, 'Mohit Sharma', 11, 'B', '2001-12-31',4500); insert into student values (333, 'K.P. Gupta', 12, 'B', '2000-01-11',3000); insert into student values (444, 'Ajit Kumar', 10, 'A', '2002-03-04',2000); insert into student values (555, 'Nandini', 12, 'C', '2000-03-04',3000); insert into student values (666, 'Rohan Sharma', 11, 'B', '2001-12-05',2500);
Explicitly Inserting NULL Values : If a column can hold NULL values, its value can be omitted from the INSERT INTO statement. You need to specify the corresponding column names whose
values are specified in INSERT INTO statement, NULL value will automatically be inserted in the
rest of the columns.
insert into student (admno, name, dob) values (777, 'Leena ', '2000-03-05');
9. SELECT: (DML) It retrieves data from a table SELECT * / columnnames / expressions as “alias name”
FROM tablename WHERE condition
ORDER BY columnname ASC/DESC;
Retrieving all Columns Ques: Display all information from student table. Select * from student;
Database Concepts
Page 5 of 19
+-------+--------------+-------+---------+------------+---------+ | admno | name | class | section | dob | fees | +-------+--------------+-------+---------+------------+---------+ | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | | 222 | Mohit Sharma | 11 | B | 2001-12-31 | 4500.00 | | 333 | K.P. Gupta | 12 | B | 2000-01-11 | 3000.00 | | 444 | Ajit Kumar | 10 | A | 2002-03-04 | 2000.00 | | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | | 666 | Rohan Sharma | 11 | B | 2001-12-05 | 2500.00 | | 777 | Leena | NULL | NULL | 2000-03-05 | NULL | +-------+--------------+-------+---------+------------+---------+
Retrieving Selected Columns
Ques: Display name and class information from student table. Select name , class from student; +--------------+-------+ | name | class | +--------------+-------+ | Anu Jain | 12 | | Mohit Sharma | 11 | | K.P. Gupta | 12 | | Ajit Kumar | 10 | | Nandini | 12 | | Rohan Sharma | 11 | | Leena | NULL | +--------------+-------+
Using Arithmetic Operators with SELECT
Using arithmetic operators on tables does not create new columns in the tables or change the
actual data values. The results of the calculations appear only in the output.
Ques: Display fees + 500 as increased fees for all students Select admno , name , fees + 500 from student; +-------+--------------+------------+ | admno | name | fees + 500 | +-------+--------------+------------+ | 111 | Anu Jain | 3000.00 | | 222 | Mohit Sharma | 5000.00 | | 333 | K.P. Gupta | 3500.00 | | 444 | Ajit Kumar | 2500.00 | | 555 | Nandini | 3500.00 | | 666 | Rohan Sharma | 3000.00 | | 777 | Leena | NULL | +-------+--------------+------------+
Note: The value of fees stored in the table remains the same.
Using Column Alias
Column alias gives a different heading to appear in output instead of the actual column. Using
Column Alias does not rename a column. It simply displays a different column name in the output. The AS keyword between the column name and alias is optional
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus ( remainder)
Database Concepts
Page 6 of 19
Ques: Display fees * 12 as annual fees for all students Select admno, name, fees * 12 as "annual fees" from student;
If a Column alias consists of more than one word, then it should be enclosed in quotes " ". +-------+--------------+--------------+ | admno | name | annual fees | +-------+--------------+--------------+ | 111 | Anu Jain | 30000.00 | | 222 | Mohit Sharma | 54000.00 | | 333 | K.P. Gupta | 36000.00 | | 444 | Ajit Kumar | 24000.00 | | 555 | Nandini | 36000.00 | | 666 | Rohan Sharma | 30000.00 | | 777 | Leena | NULL | +-------+--------------+--------------+
Ques: Display fine as 10% of fees for all students Select admno , name , fees*10/100 as fine from student; +-------+--------------+------------+ | admno | name | fine | +-------+--------------+------------+ | 111 | Anu Jain | 250.000000 | | 222 | Mohit Sharma | 450.000000 | | 333 | K.P. Gupta | 300.000000 | | 444 | Ajit Kumar | 200.000000 | | 555 | Nandini | 300.000000 | | 666 | Rohan Sharma | 250.000000 | | 777 | Leena | NULL | +-------+--------------+------------+
Putting text in Query output
Query output can be made more presentable by inserting items such as symbols or text in the query output
Ques: Display details of all students in the format : Name studies in class-Section Select name, 'studies in' , class , '-' , section from student; +--------------+------------+-------+---+---------+ | name | studies in | class | - | section | +--------------+------------+-------+---+---------+ | Anu Jain | studies in | 12 | - | A | | Mohit Sharma | studies in | 11 | - | B | | K.P. Gupta | studies in | 12 | - | B | | Ajit Kumar | studies in | 10 | - | A | | Nandini | studies in | 12 | - | C | | Rohan Sharma | studies in | 11 | - | B | | Leena | studies in | NULL | - | NULL | +--------------+------------+-------+---+---------+
Retrieving specific rows using WHERE clause
WHERE clause is used to filter records. It is used to extract only those records that fulfill a specified criterion. Where condition in the SELECT command can be formed with the following
a) Relational Operators are used to compare two values. The result of the comparison is True or
False
SELECT * / columnnames / expressions as “alias name”
FROM tablename WHERE columnname >= Value;
= Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
!= or <> Not equal to
Database Concepts
Page 7 of 19
Ques: Display students' name, who are paying below 3000 fees. Select name from student where fees < 3000;
+--------------+ | name | +--------------+ | Anu Jain | | Ajit Kumar | | Rohan Sharma | +--------------+
Ques: Display students' name, who are paying above or equal to 3000 fees. Select name from student where fees >= 3000; +--------------+ | name | +--------------+ | Mohit Sharma | | K.P. Gupta | | Nandini | +--------------+
Ques: Display students' information, who are not in class 10 Select * from student where class != 10; +-------+--------------+-------+---------+------------+---------+ | admno | name | class | section | dob | fees | +-------+--------------+-------+---------+------------+---------+ | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | | 222 | Mohit Sharma | 11 | B | 2001-12-31 | 4500.00 | | 333 | K.P. Gupta | 12 | B | 2000-01-11 | 3000.00 | | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | | 666 | Rohan Sharma | 11 | B | 2001-12-05 | 2500.00 | +-------+--------------+-------+---------+------------+---------+
b) Logical Operators ( NOT, AND, OR) : NOT negates a condition. If a condition returns a True value, adding NOT causes the condition
to return a False value and vice versa. AND and OR are used to connect relational expressions in the WHERE clause. AND requires both conditions to be true in order to return TRUE but OR
returns TRUE If any of the comparisons are true, The symbol || can be used in place of OR, && can be used in place of AND , ! can be used in place of NOT operator.
SELECT * / columnnames / expressions as “alias name”
FROM tablename WHERE NOT (condition);
SELECT * / columnnames / expressions as “alias name” FROM tablename
WHERE condition1 AND/OR condition2;
Ques: Display students' information, who are not in class 11 Select * from student where NOT (class = 11); +-------+------------+-------+---------+------------+---------+ | admno | name | class | section | dob | fees | +-------+------------+-------+---------+------------+---------+ | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | | 333 | K.P. Gupta | 12 | B | 2000-01-11 | 3000.00 | | 444 | Ajit Kumar | 10 | A | 2002-03-04 | 2000.00 | | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | +-------+------------+-------+---------+------------+---------+
Ques: Display information of students in class 11B. Select * from student where class = 11 AND section = ’B’; +-------+--------------+-------+---------+------------+---------+ | admno | name | class | section | dob | fees | +-------+--------------+-------+---------+------------+---------+ | 222 | Mohit Sharma | 11 | B | 2001-12-31 | 4500.00 | | 666 | Rohan Sharma | 11 | B | 2001-12-05 | 2500.00 | +-------+--------------+-------+---------+------------+---------+
Database Concepts
Page 8 of 19
Ques: Display 11th and 12th class students' information. Select * from student where class = 11 OR class = 12; +-------+--------------+-------+---------+------------+---------+ | admno | name | class | section | dob | fees | +-------+--------------+-------+---------+------------+---------+ | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | | 222 | Mohit Sharma | 11 | B | 2001-12-31 | 4500.00 | | 333 | K.P. Gupta | 12 | B | 2000-01-11 | 3000.00 | | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | | 666 | Rohan Sharma | 11 | B | 2001-12-05 | 2500.00 | +-------+--------------+-------+---------+------------+---------+
c) Condition based on Range : BETWEEN/NOT BETWEEN operator is used to define the range
of values within which the column values must fall to make a condition true. Range includes both the upper and the lower values.
SELECT * / columnnames / expressions as “alias name”
FROM tablename WHERE columnname BETWEEN lowerValue AND UpperValue;
Ques: Display students' information, who are paying fees between 2500 and 3500.
Select * from student where fees BETWEEN 2500 AND 3500; +-------+--------------+-------+---------+------------+---------+ | admno | name | class | section | dob | fees | +-------+--------------+-------+---------+------------+---------+ | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | | 333 | K.P. Gupta | 12 | B | 2000-01-11 | 3000.00 | | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | | 666 | Rohan Sharma | 11 | B | 2001-12-05 | 2500.00 | +-------+--------------+-------+---------+------------+---------+
d) Condition based on a List : IN/NOT IN operator is used to select values that match any value
in a list of specified values. SELECT * / columnnames / expressions as “alias name”
FROM tablename
WHERE columnname IN (value1, value2, …..valueN);
Ques: Display students' information, who are in section A, B, D and E. Select * from student where section IN ('A','B','D','E'); +-------+--------------+-------+---------+------------+---------+ | admno | name | class | section | dob | fees | +-------+--------------+-------+---------+------------+---------+ | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | | 222 | Mohit Sharma | 11 | B | 2001-12-31 | 4500.00 | | 333 | K.P. Gupta | 12 | B | 2000-01-11 | 3000.00 | | 444 | Ajit Kumar | 10 | A | 2002-03-04 | 2000.00 | | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | | 666 | Rohan Sharma | 11 | B | 2001-12-05 | 2500.00 | +-------+--------------+-------+---------+------------+---------+
e) Condition based on pattern matches : LIKE/NOT LIKE clause is used for pattern matching
of string data using wildcard characters
%( to match zero or many characters) and _( to match one character) . The symbols can be used in combinations.
Some examples : 'Am%' matches any string starting with Am.
'%Singh%' matches any string containing 'Singh' '%a' matches any string ending with 'a'
'_ _ _' matches any string that is exactly 3 characters long. '_ _ %' matches any string that has at least 2 characters.
'_ _ _ g' matches any string that is 4 characters long with any 3 characters in the
beginning but 'g' as the 4th character.
Database Concepts
Page 9 of 19
SELECT * / columnnames / expressions as “alias name”
FROM tablename WHERE columnname LIKE ‘pattern’;
Ques: Display the names that start with letter "A". select * from student where name like 'A%';
+-------+------------+-------+---------+------------+---------+ | admno | name | class | section | dob | fees | +-------+------------+-------+---------+------------+---------+ | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | | 444 | Ajit Kumar | 10 | A | 2002-03-04 | 2000.00 | +-------+------------+-------+---------+------------+---------+
Ques: Display names, whose name's second letter is 'o'. select * from student where name like '_o%'; +-------+--------------+-------+---------+------------+---------+ | admno | name | class | section | dob | fees | +-------+--------------+-------+---------+------------+---------+ | 222 | Mohit Sharma | 11 | B | 2001-12-31 | 4500.00 | | 666 | Rohan Sharma | 11 | B | 2001-12-05 | 2500.00 | +-------+--------------+-------+---------+------------+---------+
Ques: Display names, whose name has 7 characters. select * from student where name like '_ _ _ _ _ _ _'; +-------+---------+-------+---------+------------+---------+ | admno | name | class | section | dob | fees | +-------+---------+-------+---------+------------+---------+ | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | +-------+---------+-------+---------+------------+---------+
f) Checking missing values : IS NULL / IS NOT NULL is used to select rows in which the
specified column IS NULL or IS NOT NULL
SELECT * / columnnames / expressions as “alias name”
FROM tablename WHERE columnname IS NULL;
NULL represents a value that is unavailable, unassigned, unknown or inapplicable. NULL is not
same as a Zero. Since Zero (0) is an integer value but NULL is nothing. Relational Operators (= and != ) are not allowed with NULL. If any column value involved in an arithmetic expression
is NULL, the result of the arithmetic expression is also NULL.
Ques: Display students' information, whose class is missing. select * from student where class is null;
+-------+--------+-------+---------+------------+------+ | admno | name | class | section | dob | fees | +-------+--------+-------+---------+------------+------+ | 777 | Leena | NULL | NULL | 2000-03-05 | NULL | +-------+--------+-------+---------+------------+------+
Sorting the Results- ORDER BY is used to display the query output in ascending or in
descending order of a single column or multiple columns
SELECT * / columnnames / expressions as “alias name” FROM tablename
WHERE condition ORDER BY columnname1 ASC/DESC , columnname2 ASC/DESC;
Ques: Display students' information, in descending order of fees
Select * from student order by fees desc;
Database Concepts
Page 10 of 19
+-------+--------------+-------+---------+------------+---------+ | admno | name | class | section | dob | fees | +-------+--------------+-------+---------+------------+---------+ | 222 | Mohit Sharma | 11 | B | 2001-12-31 | 4500.00 | | 333 | K.P. Gupta | 12 | B | 2000-01-11 | 3000.00 | | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | | 666 | Rohan Sharma | 11 | B | 2001-12-05 | 2500.00 | | 444 | Ajit Kumar | 10 | A | 2002-03-04 | 2000.00 | | 777 | Leena | NULL | NULL | 2000-03-05 | NULL | +-------+--------------+-------+---------+------------+---------+
Ques: Display students' information, in order of class and section.
Select * from student order by class, section; +-------+--------------+-------+---------+------------+---------+ | admno | name | class | section | dob | fees | +-------+--------------+-------+---------+------------+---------+ | 777 | Leena | NULL | NULL | 2000-03-05 | NULL | | 444 | Ajit Kumar | 10 | A | 2002-03-04 | 2000.00 | | 222 | Mohit Sharma | 11 | B | 2001-12-31 | 4500.00 | | 666 | Rohan Sharma | 11 | B | 2001-12-05 | 2500.00 | | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | | 333 | K.P. Gupta | 12 | B | 2000-01-11 | 3000.00 | | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | +-------+--------------+-------+---------+------------+---------+
Sorting on Column Alias: If a Column alias is defined on a column, we can use it for displaying rows in an ascending or descending order using ORDER BY clause:
SELECT columnname1, columnname2 as alias FROM tablename
WHERE condition ORDER BY alias;
Eliminating duplicate values : DISTINCT clause is used to display unique values from a particular column of a table.
SELECT DISTINCT (column) FROM tablename ;
DISTINCT can be used with only one column at a time. When DISTINCT keyword is specified, only one instance of the duplicated data is shown. If we write the keyword ALL in place of
DISTINCT, then the result of SELECT query displays all the values including duplicate values
Ques: Display different classes from student table.
Select distinct class from student; +-------+ | class | +-------+ | 12 | | 11 | | 10 | | NULL | +-------+
Ques: Display class in student table. Select All class from student; +-------+ | class | +-------+ | 12 | | 11 | | 12 | | 10 | | 12 | | 11 | | NULL | +-------+
Database Concepts
Page 11 of 19
Precedence of Operators
1 !
2 -(unary minus)
3 ^
4 *, /, DIV, %, MOD
5 -, +
6 =, <=>, >=, >, <=, <, <>, !=, IS, LIKE, IN
7 BETWEEN,
8 NOT
9 &&, AND
10 ||, OR
11 = (assignment)
10. UPDATE: (DML) It updates/modifies data values in a table. UPDATE statement can be used to
update one or more columns together. WHERE clause helps in updating of particular rows in a table
UPDATE tablename
SET columnname1 = value1, columnname2 = value2… WHERE condition;
Ques: Increase fees value by 500 for all students Update student set fees = fees + 500;
Ques: Increase the fees value by 100 for admno 222 Update student set fees = fees + 100 where admno = 222;
11. DELETE :(DML)It Deletes a Row from a table. DELETE removes the entire row, not the individual
column values DELETE FROM tablename WHERE condition;
Ques: Remove admno 444 information. Delete from student where admno = 444;
DELETE can be used to delete all rows of the table by skipping the where clause.
Ques: Remove all records. Delete from student;
12. ALTER TABLE :(DDL) It is used to restructure the table.
To Add a new Column in The Table
ALTER TABLE tablename ADD columnname datatype(size);
To Remove an existing Column from A Table
ALTER TABLE tablename DROP columnname;
To Modify the data type or size of a Column
ALTER TABLE tablename MODIFY columnname newdatatype(newsize);
Ques: Add one new column totalfees with numeric(10,2)
Alter table student add totalfees numeric(10,2);
After execution of the above ALTER TABLE statement, the totalfees column is added and a NULL value is automatically assigned to all the existing rows in this column.
Ques: Change totalfees datatype as numeric(12,2) Alter table student modify totalfees numeric(12,2);
Ques: Remove totalfees column. Alter table student drop totalfees;
Database Concepts
Page 12 of 19
13. DROP TABLE :(DDL)It removes the entire structure of the table and information DROP TABLE tablename;
Example: Drop table student;
14. DROP DATABASE :(DDL)It removes the entire database from system. DROP DATABASE databasename;
Ques: Differentiate between Update and Alter commands
Alter Table Update
It is a DDL statement It is a DML statement
Used to restructure the table Used to change the data values in specified
columns of all those records which satisfy the specified condition
Ques: Differentiate between Delete and Drop commands
Drop Table Delete
It is a DDL statement It is a DML statement
Used to remove entire table structure with contents
Used to remove the records which satisfy the specified condition
Ques: Explain Multiple Row Functions / Aggregate Functions used in MySQL.
Multiple Row Function works on multiple values and returns an aggregate result for the entire group. These functions are also called Aggregate Functions or Group
Functions. Some Aggregate functions in MySQL are:
1. MAX(column) Returns the maximum of the non-NULL values of a particular column.
2. MIN(column) Returns the minimum of the non-NULL values of a particular column.
3. AVG(column) Returns the average of the non-NULL values of a particular column. The
AVG(column) function can be applied on numeric (int/decimal) type only. Averages of
String and Date type data are not defined.
4. SUM(column) Returns the sum of the non-NULL values of a particular column. The
SUM(column) function can be applied on numeric (int/decimal) type only. Sums of String
and Date type data are not defined.
5. COUNT(column) Returns the count of the number of non-NULL of a particular column.
If the argument is an *, then it counts the total number of rows satisfying the
condition in the entire table.
Syntax :
SELECT AggregateFunction( distinct ColumnName) FROM TableName ;
Note :
Aggregate functions does not take NULL into consideration and ignores it.
WHERE condition can be specified to restrict the rows from calculations in Aggregate
functions.
If you want to exclude duplicate values from the calculations in Aggregate functions,
use the DISTINCT keyword. The ALL keyword includes even duplicates. If nothing is
specified, then ALL is assumed as the default
count(*) counts the number of records in the whole table and not any particular
column
Database Concepts
Page 13 of 19
Consider the table Student shown below: +-------+--------------+-------+---------+------------+---------+ | admno | name | class | section | dob | fees | +-------+--------------+-------+---------+------------+---------+ | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | | 222 | Mohit Sharma | 11 | B | 2001-12-31 | 4500.00 | | 333 | K.P. Gupta | 12 | B | 2000-01-11 | 3000.00 | | 444 | Ajit Kumar | 10 | A | 2002-03-04 | 2000.00 | | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | | 666 | Rohan Sharma | 11 | B | 2001-12-05 | 2500.00 | | 777 | Leena | NULL | NULL | 2000-03-05 | NULL | +-------+--------------+-------+---------+------------+---------+
Ques: Display the total fees collected from students. select sum(fees) from student;
+-----------+ | sum(fees) | +-----------+ | 17500.00 | +-----------+
Ques: Display the average fees of students select avg(fees) from student;
+-------------+ | avg(fees) | +-------------+ | 2916.666667 | +-------------+
Ques: Display the highest and the lowest fees paid by students select max(fees), min(fees) from student;
+-----------+-----------+ | max(fees) | min(fees) | +-----------+-----------+ | 4500.00 | 2000.00 | +-----------+-----------+
Ques: Count the number of values in fees select count(fees) from student;
+-------------+ | count(fees) | +-------------+ | 6 | +-------------+
Ques: Count the number of records in the table select count(*) from student;
+----------+ | count(*) | +----------+ | 7 | +----------+
Ques: Count the number of different sections select count( distinct section) from student;
+--------------------------+ | count( distinct section) | +--------------------------+ | 3 | +--------------------------+
Database Concepts
Page 14 of 19
Ques: What is the use of Group By clause in MySQL?
GROUP BY clause is used in the SELECT statement to arrange the records of a table
into groups based on unique values in the columns. It group rows with same column
values, so that the aggregate functions can now be applied to separately to each group instead of the whole table. When Group by is used aggregate functions return
one result for each group.
Syntax :
SELECT GroupingColumn , AggregateFunction(columnname)
FROM Tablename GROUP BY GroupingColumn;
Grouping using Multiple Columns: SELECT GroupingColumn1 , GroupingColumn2 , AggregateFunction(columnname) FROM Tablename GROUP BY GroupingColumn1 , GroupingColumn2;
Note: When Grouping is done using multiple columns, The GROUP BY clause operates on both
GroupingColumn1 and GroupingColumn2 to identify unique column combinations and
make groups of those rows.
Ques: Display number of students in each class. select class , count(*) from student group by class; +-------+----------+ | class | count(*) | +-------+----------+ | NULL | 1 | | 10 | 1 | | 11 | 2 | | 12 | 3 | +-------+----------+
Ques: Display number of students in each section of each class. select class , section , count(*) from student group by class, section; +-------+---------+----------+ | class | section | count(*) | +-------+---------+----------+ | NULL | NULL | 1 | | 10 | A | 1 | | 11 | B | 2 | | 12 | A | 1 | | 12 | B | 1 | | 12 | C | 1 | +-------+---------+----------+
Ques: Display sum of fees for each class. select class , sum(fees) from student group by class;
+-------+-----------+ | class | sum(fees) | +-------+-----------+ | NULL | NULL | | 10 | 2000.00 | | 11 | 7000.00 | | 12 | 8500.00 | +-------+-----------+
Ques: What is the use of Having clause in MySQL?
HAVING is used to place condition on groups returned by 'GROUP BY' clause. It is
used when we want to see the output only for those groups which satisfy some condition.
Syntax:
SELECT GroupingColumn , AggregateFunction( columnname )
FROM Tablename GROUP BY GroupingColumn
HAVING Groupcondition;
Note: Having condition can be formed using any columns returned by the Group By clause
or any AggrgateFunction applied on these groups.
Database Concepts
Page 15 of 19
Ques: Display sum of fees which is more than 5000 for each class select class , sum(fees) from student group by class having sum(fees)>5000; +-------+-----------+ | class | sum(fees) | +-------+-----------+ | 11 | 7000.00 | | 12 | 8500.00 | +-------+-----------+
Ques: Display average fees for those classes where number of students is more than 1 select class , avg(fees) from student group by class having count(*)>1; +-------+-------------+ | class | avg(fees) | +-------+-------------+ | 11 | 3500.000000 | | 12 | 2833.333333 | +-------+-------------+
Ques: Display sum of fees for class =10; select class , sum(fees) from student group by class having class != 11; +-------+-----------+ | class | sum(fees) | +-------+-----------+ | 10 | 2000.00 | | 12 | 8500.00 | +-------+-----------+
Ques: What is the Cartesian Product of two tables?
CARTESIAN PRODUCT (OR CROSS JOIN) of two tables is a table obtained by joining every row of first table with each row of the second table. A Cartesian product of two
tables contains all the columns from both the tables. No. of Rows in Cartesian Product = No. of Rows in Table1 X No. of Rows in Table2 (Product of Rows)
No. of columns in Cartesian Product = No. of Cols in Table1 + No. of Cols in Table2 (Sum of Columns)
Syntax:
SELECT * FROM table1 , table2 ;
Cartesian product of two tables is obtained by giving the names of both tables in FROM
clause of the SELECT query.
Ques: Display the Cartesian product of student and Library tables shown below : STUDENT
+-------+--------------+-------+---------+------------+---------+ | admno | name | class | section | dob | fees | +-------+--------------+-------+---------+------------+---------+ | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | | 222 | Mohit Sharma | 11 | B | 2001-12-31 | 4500.00 | | 333 | K.P. Gupta | 12 | B | 2000-01-11 | 3000.00 | | 444 | Ajit Kumar | 10 | A | 2002-03-04 | 2000.00 | | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | | 666 | Rohan Sharma | 11 | B | 2001-12-05 | 2500.00 | | 777 | Leena | NULL | NULL | 2000-03-05 | NULL | +-------+--------------+-------+---------+------------+---------+
LIBRARY +--------+--------------------+-------+-------------+ | BookNo | BookName | Admno | DateofIssue | +--------+--------------------+-------+-------------+ | 1001 | Harry Potter | 555 | 2017-03-01 | | 1002 | A Brave New World | 111 | 2017-03-05 | | 1003 | The Alchemist | 444 | 2017-03-07 | | 1004 | The Da Vinci Code | 222 | 2017-03-11 | | 1005 | Lord of The Rings | 555 | 2017-03-17 | | 1006 | Gone with the Wind | 111 | 2017-03-21 | +--------+--------------------+-------+-------------+
Database Concepts
Page 16 of 19
The Cartesian Product of tables Student and Library can be obtained by the following
command:
select * from student , library;
+-------+--------------+-------+---------+------------+---------+--------+--------------------+-------+-------------+ | admno | name | class | section | dob | fees | BookNo | BookName | Admno | DateofIssue | +-------+--------------+-------+---------+------------+---------+--------+--------------------+-------+-------------+ 1. | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | 1001 | Harry Potter | 555 | 2017-03-01 | 2. | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | 1002 | A Brave New World | 111 | 2017-03-05 | 3. | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | 1003 | The Alchemist | 444 | 2017-03-07 | 4. | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | 1004 | The Da Vinci Code | 222 | 2017-03-11 | 5. | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | 1005 | Lord of The Rings | 555 | 2017-03-17 | 6. | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | 1006 | Gone with the Wind | 111 | 2017-03-21 | 7. | 222 | Mohit Sharma | 11 | B | 2001-12-31 | 4500.00 | 1001 | Harry Potter | 555 | 2017-03-01 | 8. | 222 | Mohit Sharma | 11 | B | 2001-12-31 | 4500.00 | 1002 | A Brave New World | 111 | 2017-03-05 | 9. | 222 | Mohit Sharma | 11 | B | 2001-12-31 | 4500.00 | 1003 | The Alchemist | 444 | 2017-03-07 | 10. | 222 | Mohit Sharma | 11 | B | 2001-12-31 | 4500.00 | 1004 | The Da Vinci Code | 222 | 2017-03-11 | 11. | 222 | Mohit Sharma | 11 | B | 2001-12-31 | 4500.00 | 1005 | Lord of The Rings | 555 | 2017-03-17 | 12. | 222 | Mohit Sharma | 11 | B | 2001-12-31 | 4500.00 | 1006 | Gone with the Wind | 111 | 2017-03-21 | 13. | 333 | K.P. Gupta | 12 | B | 2000-01-11 | 3000.00 | 1001 | Harry Potter | 555 | 2017-03-01 | 14. | 333 | K.P. Gupta | 12 | B | 2000-01-11 | 3000.00 | 1002 | A Brave New World | 111 | 2017-03-05 | 15. | 333 | K.P. Gupta | 12 | B | 2000-01-11 | 3000.00 | 1003 | The Alchemist | 444 | 2017-03-07 | 16. | 333 | K.P. Gupta | 12 | B | 2000-01-11 | 3000.00 | 1004 | The Da Vinci Code | 222 | 2017-03-11 | 17. | 333 | K.P. Gupta | 12 | B | 2000-01-11 | 3000.00 | 1005 | Lord of The Rings | 555 | 2017-03-17 | 18. | 333 | K.P. Gupta | 12 | B | 2000-01-11 | 3000.00 | 1006 | Gone with the Wind | 111 | 2017-03-21 | 19. | 444 | Ajit Kumar | 10 | A | 2002-03-04 | 2000.00 | 1001 | Harry Potter | 555 | 2017-03-01 | 20. | 444 | Ajit Kumar | 10 | A | 2002-03-04 | 2000.00 | 1002 | A Brave New World | 111 | 2017-03-05 | 21. | 444 | Ajit Kumar | 10 | A | 2002-03-04 | 2000.00 | 1003 | The Alchemist | 444 | 2017-03-07 | 22. | 444 | Ajit Kumar | 10 | A | 2002-03-04 | 2000.00 | 1004 | The Da Vinci Code | 222 | 2017-03-11 | 23. | 444 | Ajit Kumar | 10 | A | 2002-03-04 | 2000.00 | 1005 | Lord of The Rings | 555 | 2017-03-17 | 24. | 444 | Ajit Kumar | 10 | A | 2002-03-04 | 2000.00 | 1006 | Gone with the Wind | 111 | 2017-03-21 | 25. | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | 1001 | Harry Potter | 555 | 2017-03-01 | 26. | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | 1002 | A Brave New World | 111 | 2017-03-05 | 27. | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | 1003 | The Alchemist | 444 | 2017-03-07 | 28. | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | 1004 | The Da Vinci Code | 222 | 2017-03-11 | 29. | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | 1005 | Lord of The Rings | 555 | 2017-03-17 | 30. | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | 1006 | Gone with the Wind | 111 | 2017-03-21 | 31. | 666 | Rohan Sharma | 11 | B | 2001-12-05 | 2500.00 | 1001 | Harry Potter | 555 | 2017-03-01 | 32. | 666 | Rohan Sharma | 11 | B | 2001-12-05 | 2500.00 | 1002 | A Brave New World | 111 | 2017-03-05 | 33. | 666 | Rohan Sharma | 11 | B | 2001-12-05 | 2500.00 | 1003 | The Alchemist | 444 | 2017-03-07 | 34. | 666 | Rohan Sharma | 11 | B | 2001-12-05 | 2500.00 | 1004 | The Da Vinci Code | 222 | 2017-03-11 | 35. | 666 | Rohan Sharma | 11 | B | 2001-12-05 | 2500.00 | 1005 | Lord of The Rings | 555 | 2017-03-17 | 36. | 666 | Rohan Sharma | 11 | B | 2001-12-05 | 2500.00 | 1006 | Gone with the Wind | 111 | 2017-03-21 | 37. | 777 | Leena | NULL | NULL | 2000-03-05 | NULL | 1001 | Harry Potter | 555 | 2017-03-01 | 38. | 777 | Leena | NULL | NULL | 2000-03-05 | NULL | 1002 | A Brave New World | 111 | 2017-03-05 | 39. | 777 | Leena | NULL | NULL | 2000-03-05 | NULL | 1003 | The Alchemist | 444 | 2017-03-07 | 40. | 777 | Leena | NULL | NULL | 2000-03-05 | NULL | 1004 | The Da Vinci Code | 222 | 2017-03-11 | 41. | 777 | Leena | NULL | NULL | 2000-03-05 | NULL | 1005 | Lord of The Rings | 555 | 2017-03-17 | 42. | 777 | Leena | NULL | NULL | 2000-03-05 | NULL | 1006 | Gone with the Wind | 111 | 2017-03-21 | +-------+--------------+-------+---------+------------+---------+--------+--------------------+-------+-------------+
Table 1: Student Number of rows (cardinality) = 7
Number of columns (degree) = 6
Table 2: Library
Number of rows (cardinality) = 6
Number of columns (degree) = 4 Cartesian product: Student X Library
Number of rows (cardinality) = 7 * 6 = 42
Number of columns (degree) = 6 + 4 = 10
Ques: What is an Equi-Join of two tables?
SQL Joins are used to relate information in different tables.
An EQUI-JOIN of two tables is obtained by putting an Equality condition on the
Cartesian Product of two tables in order to extract meaningful data from the
underlying tables. This equality condition is put on the common column of the two tables which have similar data and the same data types. This common column is, generally,
the primary key of one table and foreign key of the other table. We specify both
table names in the FROM clause of SELECT command and also give the Join Condition
specifying the equality of matching columns.
Syntax:
SELECT * FROM table1 A , table2 B
WHERE A.PrimaryKey = B.ForeignKey
AND AdditionalCondition;
Database Concepts
Page 17 of 19
STUDENT +-------+--------------+-------+---------+------------+---------+ | admno | name | class | section | dob | fees | +-------+--------------+-------+---------+------------+---------+ | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | | 222 | Mohit Sharma | 11 | B | 2001-12-31 | 4500.00 | | 333 | K.P. Gupta | 12 | B | 2000-01-11 | 3000.00 | | 444 | Ajit Kumar | 10 | A | 2002-03-04 | 2000.00 | | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | | 666 | Rohan Sharma | 11 | B | 2001-12-05 | 2500.00 | | 777 | Leena | NULL | NULL | 2000-03-05 | NULL | +-------+--------------+-------+---------+------------+---------+
LIBRARY +--------+--------------------+-------+-------------+ | BookNo | BookName | Admno | DateofIssue | +--------+--------------------+-------+-------------+ | 1001 | Harry Potter | 555 | 2017-03-01 | | 1002 | A Brave New World | 111 | 2017-03-05 | | 1003 | The Alchemist | 444 | 2017-03-07 | | 1004 | The Da Vinci Code | 222 | 2017-03-11 | | 1005 | Lord of The Rings | 555 | 2017-03-17 | | 1006 | Gone with the Wind | 111 | 2017-03-21 | +--------+--------------------+-------+-------------+
Ques: Display student details with corresponding book details select * from student , library where student.admno = library.admno;
+-------+--------------+-------+---------+------------+---------+--------+--------------------+-------+-------------+ | admno | name | class | section | dob | fees | BookNo | BookName | Admno | DateofIssue | +-------+--------------+-------+---------+------------+---------+--------+--------------------+-------+-------------+ | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | 1001 | Harry Potter | 555 | 2017-03-01 | | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | 1002 | A Brave New World | 111 | 2017-03-05 | | 444 | Ajit Kumar | 10 | A | 2002-03-04 | 2000.00 | 1003 | The Alchemist | 444 | 2017-03-07 | | 222 | Mohit Sharma | 11 | B | 2001-12-31 | 4500.00 | 1004 | The Da Vinci Code | 222 | 2017-03-11 | | 555 | Nandini | 12 | C | 2000-03-04 | 3000.00 | 1005 | Lord of The Rings | 555 | 2017-03-17 | | 111 | Anu Jain | 12 | A | 2000-12-31 | 2500.00 | 1006 | Gone with the Wind | 111 | 2017-03-21 | +-------+--------------+-------+---------+------------+---------+--------+--------------------+-------+-------------+
Ques: Display admno, name of student, bookname and issue date select student.admno,name,bookname,dateofissue from student,library where student.admno = library.admno; +-------+--------------+--------------------+-------------+ | admno | name | bookname | dateofissue | +-------+--------------+--------------------+-------------+ | 555 | Nandini | Harry Potter | 2017-03-01 | | 111 | Anu Jain | A Brave New World | 2017-03-05 | | 444 | Ajit Kumar | The Alchemist | 2017-03-07 | | 222 | Mohit Sharma | The Da Vinci Code | 2017-03-11 | | 555 | Nandini | Lord of The Rings | 2017-03-17 | | 111 | Anu Jain | Gone with the Wind | 2017-03-21 | +-------+--------------+--------------------+-------------+
Note :
If a column with the same name exists in both the tables, then it is mandatory to prefix the column name with the table name from which it is to be selected in order to eliminate
ambiguity.
Ques: Display name, class, section, dateofissue from student and library tables for books
issued after 2017-03-15
Select name , class , section , dateofissue from student,library where student.admno = library.admno and dateofissue > '2017-03-15'; +----------+-------+---------+-------------+ | name | class | section | dateofissue | +----------+-------+---------+-------------+ | Nandini | 12 | C | 2017-03-17 | | Anu Jain | 12 | A | 2017-03-21 | +----------+-------+---------+-------------+
Database Concepts
Page 18 of 19
Note :
Additional conditions can also be specified along with the Joining Condition in the where
clause while making a Join
Ques: Display admno, name, bookname for students of class 12 select S.admno, name , bookname from student S , library L where S.admno = L.admno and class = 12;
+-------+----------+--------------------+ | admno | name | bookname | +-------+----------+--------------------+ | 555 | Nandini | Harry Potter | | 111 | Anu Jain | A Brave New World | | 555 | Nandini | Lord of The Rings | | 111 | Anu Jain | Gone with the Wind | +-------+----------+--------------------+
Note :
Alias names can be given to tables in the From clause while making a Join. You can then
refer to the tables only by their alias.
Ques: Display name, class, section of student who has issued book ‘The Da Vinci Code’ select name , class , section from student S , library L where S.admno = L.admno and bookname = 'The Da Vinci Code';
+--------------+-------+---------+ | name | class | section | +--------------+-------+---------+ | Mohit Sharma | 11 | B | +--------------+-------+---------+
Ques: What is Foreign Key?
Foreign key is a key in a table which derives its values from the Primary Key column
of some other table. It is used to ensure referential integrity and to get Equi-Join of two tables.
Ques: What is Referential Integrity?
Referential Integrity is the property of a relational database which ensures that no
entry in a foreign key column of a table can be made unless it matches a primary key
value in the corresponding related table.
Ques: What is a Union? Union is an operation of combining the output of two SELECT statements. Union of two
SELECT statements can be performed only if their outputs contain same number of
columns and data types of corresponding columns are also the same.
Syntax:
SELECT col1 , col2… FROM table1 UNION ALL SELECT col1 , col2… FROM table2; Note :
Union does not display any duplicate rows unless ALL is specified with it.
Ques: Write a command to create a union of Student1 and Student2? Table: Student1 Table: Student2
Rollno Name
11 Kumar
22 Mohan
33 Rohit
Resultant Union of the Two Tables is: SELECT * FROM Student1 UNION SELECT * FROM Student2;
Rollno Name
22 Mohan
11 Rahul
77 Kavita
Rollno Name
11 Kumar
22 Mohan
33 Rohit
11 Rahul
77 Kavita
Database Concepts
Page 19 of 19
Ques: What is Relational Algebra?
Relational algebra consists of a set of operations that take one or two relations as
input and produces a new relation as output.
Operation in relational algebra are : Selection, Projection, Union, Cartesian Product
Ques: What do you understand by the Selection and Projection operation in Relational
Algebra ? Explain by an Example
Consider the following Relation
Relation: Student ADNO NAME CLASS SECTION AVERAGE
101 Anu 12 A 85
105 Balu 12 D 65
203 Leena 11 B 95
205 Madhu 10 B 75
305 Surpreeth 9 C 70
483 Usha 6 A 60
Selection in relational algebra returns those
tuples(records) in a relation that fulfil a
condition
(Produce a table containing subset of rows).
Syntax: σ condition (relation)
σ class=12 (Student)
101 Anu 12 A 85
105 Balu 12 D 65
Projection in relational algebra returns
those columns in a relation that are given in
the attribute list
(Produce table containing subset of columns).
Syntax: ∏ attribute list (relation)
∏ Adno,Name (Student)
ADNO NAME
101 Anu
105 Balu
203 Leena
205 Madhu
305 Surpreeth
483 Usha
Ques: What is the Union of two tables?
The Union operator is used to combine two or more tables. Each table within the UNION
should have the same number of columns, similar data types and also the columns
must be in the same order. In the union operation, duplicate records will be
automatically removed from the resultant table.
Table: Student1 Table: Student2 Rollno Name
11 Kumar
22 Mohan
33 Rohit
Rollno Name
22 Mohan
11 Rahul
77 Kavita
σ (Students 1)
Union σ (Students 2)
∏ rollno, Name (Students 1)
Union
∏ rollno, Name (Students 2)
Resultant Union
of the Two
Tables is:
Rollno Name
11 Kumar
22 Mohan
33 Rohit
11 Rahul
77 Kavita
In the above resultant table, student1 is
copied as it is, but in student2, roll no 22
Mohan's information is same as student1. So, that is not copied in the resultant table again.
Roll no 11 is same as student1, but name is
different. So, that is copied in the resultant
table. Roll no 77 is not in student1 table, so
that is also copied in the resultant table.