316 Queries
316 Queries
We will present a number of sample queries using the following table definitions:
Queries:
CREATE TABLE sailors (sid int PRIMARY KEY, sname varchar (50), rating
integer ,age int)
CREATE TABLE boats(bid int PRIMARY KEY,bname varchar(50),color
varchar(50));
CREATE TABLE reserves (sid int,bid int,day date,PRIMARY KEY
(sid,bid,day) ,FOREIGN KEY(sid) REFERENCES sailors(sid), FOREIGN
KEY(bid) REFERENCES boats(bid));
Example:
Example:
Sailors
UNION:
Union is an operator that allows us to combine two or more results from multiple
SELECT queries into a single result set. It comes with a default feature that removes
the duplicate rows from the result set. MySQL always uses the name of the column
in the first SELECT statement will be the column names of the result set (output).
The number and order of the columns should be the same in all tables that
you are going to use.
The data type must be compatible with the corresponding positions of each
select query.
The column name selected in the different SELECT queries must be in the
same order.
Syntax:
SELECT expression1, expression2, expression_n
FROM tables
[WHERE conditions]
UNION
SELECT expression1, expression2, expression_n
FROM tables
[WHERE conditions];
Q: Find the sid’s and names of sailors who have reserved a red or a green boat.
A: SELECT S.sid,S.sname
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = 'red'
union
SELECT S2.sid,S2.sname
FROM Sailors S2, Boats B2, Reserves R2
WHERE S2.sid = R2.sid AND R2.bid = B2.bid AND B2.color = 'green';
INTERSECT:
The INTERSECT operator is used to fetch the records that are in common between
two SELECT statements or data sets. If a record exists in one query and not in the
other, it will be omitted from the INTERSECT results.
Syntax:
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
INTERSECT
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];
The number and order of the columns should be the same in all tables that
you are going to use.
The data type must be compatible with the corresponding positions of each
select query.
The column name selected in the different SELECT queries must be in the
same order.
Q: Find the sid’s and names of sailors who have reserved a red and a green boat.
A: SELECT S.sid,S.sname
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = 'red'
INTERSECT
SELECT S2.sid,S2.sname
FROM Sailors S2, Boats B2, Reserves R2
WHERE S2.sid = R2.sid AND R2.bid = B2.bid AND B2.color = 'green';
EXCEPT:
The EXCEPT clause in SQL is widely used to filter records from more than one
table. This statement first combines the two SELECT statements and returns records
from the first SELECT query, which isn’t present in the second SELECT query's
result. In other words, it retrieves all rows from the first SELECT query while
deleting redundant rows from the second.
This statement behaves the same as the minus (set difference) operator does in
mathematics.
Q: Find the sids of all sailors who have reserved red boats but not green boats.
A: SELECT S.sid
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = 'red'
MINUS // minus in Oracle, Except in SQL server
SELECT S2.sid
FROM Sailors S2, Boats B2, Reserves R2
WHERE S2.sid = R2.sid AND R2.bid = B2.bid AND B2.color = 'green';
Nested Queries
A nested query is a query that has another query embedded within it; the embedded
query is called a sub query. A sub query is known as the inner query, and the query
that contains sub query is known as the outer query.
Syntax:
As per the execution process of sub query, it again classified in two ways.
2. Correlated Queries.
In correlated queries, First Outer query is executed and later Inner query will
execute. i.e Inner query always depends on the result of outer query.
(Q) Find the names of sailors who have reserved a red boat.
SQL NOT IN condition used to exclude the defined multiple value in a WHERE
clause condition.
Example:
(Q) Find the names of sailors who have reserved boat number 103
SELECT S.sname FROM Sailors S WHERE
EXISTS (SELECT * FROM Reserves R WHERE R.bid = 103 AND R.sid = S.sid)
Aggregation Operators:
Group By clause:
The GROUP BY Statement in SQL is used to arrange identical data into groups with
the help of some functions. i.e if a particular column has same values in different
rows then it will arrange these rows in a group.
Important Points:
GROUP BY clause is used with the SELECT statement.
In the query, GROUP BY clause is placed after the WHERE clause.
In the query, GROUP BY clause is placed before ORDER BY clause if used
any.
Syntax:
SELECT column1, function_name(column2)
FROM table_name
WHERE condition
GROUP BY column1, column2
ORDER BY column1, column2;
Examples:
Having Clause:
The HAVING clause is like WHERE but operates on grouped records returned by
a GROUP BY. HAVING applies to summarized group records, whereas WHERE
applies to individual records. Only the groups that meet the HAVING criteria will
be returned.
NULL
We have assumed that column values in a row are always known. In practice column
values can be unknown. For example, when a sailor, say Dan, joins a yacht club, he
may not yet have a rating assigned. Since the definition for the Sailors table has a
rating column, what row should we insert for Dan? What is needed here is a special
value that denotes unknown. Suppose the Sailor table definition was modified to
include a maiden-name column. However, only married women who take their
husband's last name have a maiden name. For women who do not take their husband's
name and for men, the maiden-name column is inapplicable. Again, what value do we
include in this column for the row representing Dan? SQL provides a special column
value called null to use in such situations. We use null when the column value is either
Unknown or inapplicable. Using our Sailor table definition, we might enter the row
(98. Dan, null, 39) to represent Dan. The presence of null values complicates many
issues, and we consider the impact of null values on SQL in this section.
JOINS
OUTER JOIN:
In an outer join, along with tuples that satisfy the matching criteria, we also include
some or all tuples that do not match the criteria.
Example:
Student Marks Student Marks
In the right outer join, operation allows keeping all tuple in the right relation.
However, if there is no matching tuple is found in the left relation, then the attributes
of the left relation in the join result are filled with null values.
Example:
Student Marks Student Marks
In a full outer join, all tuples from both relations are included in the result,
irrespective of the matching condition.