0% found this document useful (0 votes)
33 views

DQL (Data Query Language)

DQL stands for Data Query Language. DQL statements consist of a SELECT command along with other elements and fetch tuples from tables, returning them as a result set. The document provides examples of basic DQL queries using operators like WHERE and different ways to apply constraints in queries.

Uploaded by

Tony Stark
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

DQL (Data Query Language)

DQL stands for Data Query Language. DQL statements consist of a SELECT command along with other elements and fetch tuples from tables, returning them as a result set. The document provides examples of basic DQL queries using operators like WHERE and different ways to apply constraints in queries.

Uploaded by

Tony Stark
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 128

DQL (Data Query Language)

DQL - Introduction
SQL | DQL - Introduction

DQL stands for Data Query Language.

DQL statements consist of a SELECT command along with a few more elements which define how the SELECT command
will be executed and what will be the format of the output.

DQL statements (also called SELECT statements) fetch tuples from one or more tables and return the data as a Result
Set. Result Set is a two-dimensional array similar to a table.
SQL | DQL - Introduction

Sample Database
Catalog Table
Supplier Table Parts Table supp_id prts_id
supp_id supp_name prts_id prts_color prts_name prts_price 101 200
101 Sunil 200 red p1 10 101 201
102 Ravi 201 green p2 20 102 203
103 Shyam 202 red p2 20 102 201
104 Arya 203 green p4 25 103 201
104 202

Sample Query
Result_Set
Details of those suppliers prts_id prts_color supp_id supp_name
who are supplying red parts 200 red 101 Sunil
202 red 104 Arya
Our First Query
DQL | Our First Query

The General Format of Query Selecting One Column

SELECT column_one, column_two, ... SELECT prts_id


FROM table_name; FROM Parts;

Parts Result_Set
prts_id prts_color prts_name prts_price prts_id
200 red p1 10 200
201 green p2 20 201
202 red p2 20 202
203 green p4 25 203
DQL | Our First Query

The General Format of Query Selecting More than One Column

SELECT column_one, column_two, ... SELECT prt_id, prt_color


FROM table_name; FROM Parts;

Parts Result_Set
prts_id prts_color prts_name prts_price prts_id prts_color
200 red p1 10 200 red
201 green p2 20 201 green
202 red p2 20 202 red
203 green p4 25 203 green
DQL | Our First Query

The General Format of Query Selecting All Columns

SELECT column_one, column_two, ... SELECT *


FROM table_name; FROM Parts;

Parts Result_Set
prts_id prts_color prts_name prts_price prts_id prts_color prts_name prts_price
200 red p1 10 200 red p1 10
201 green p2 20 201 green p2 20
202 red p2 20 202 red p2 20
203 green p4 25 203 green p4 25
Applying Constraints
DQL | Applying Constraints

General Format of Application of WHERE

SELECT column_one, column_two, ...


FROM table_name
WHERE condition_one AND/OR condition_two ...;

WHERE with Single Constraint

SELECT column_one, column_two, ...


FROM table_name
WHERE condition;
DQL | Applying Constraints

WHERE with Single Constraint


Parts
SELECT column_one, column_two, ... prts_id prts_color prts_name prts_price
FROM table_name 200 red p1 10
WHERE condition;
201 green p2 20
202 red p2 20
203 green p4 25

Result_Set
SELECT prts_id prts_id
FROM Parts 200
WHERE prts_color = 'red';
202
DQL | Applying Constraints

WHERE with Multiple Constraints


Parts
SELECT column_one, column_two, ... prts_id prts_color prts_name prts_price
FROM table_name 200 red p1 10
WHERE condition_one AND/OR condition_two ...;
201 green p2 20
202 red p2 20
203 green p4 25

Result_Set
SELECT prts_id
FROM Parts prts_id
WHERE prts_color = 'red' AND prts_price < 20; 200
DQL | Applying Constraints

Most Common Logical Operators

Logical Operator Explanation Example


=, !=, < <=, >, >= Standard numerical operators col_name != 4
DQL | Applying Constraints

Most Common Logical Operators

Logical Operator Explanation Example


=, !=, < <=, >, >= Standard numerical operators col_name != 4
BETWEEN ... AND ... Number is within range of two values col_name BETWEEN 1.5 AND 10.5
(inclusive)
DQL | Applying Constraints

Most Common Logical Operators

Logical Operator Explanation Example


=, !=, < <=, >, >= Standard numerical operators col_name != 4
BETWEEN ... AND ... Number is within range of two values col_name BETWEEN 1.5 AND 10.5
(inclusive)
NOT BETWEEN ... AND ... Number is not within range of two col_name NOT BETWEEN 1 AND 10
values (inclusive)
DQL | Applying Constraints

Most Common Logical Operators

Logical Operator Explanation Example


=, !=, < <=, >, >= Standard numerical operators col_name != 4
BETWEEN ... AND ... Number is within range of two values col_name BETWEEN 1.5 AND 10.5
(inclusive)
NOT BETWEEN ... AND ... Number is not within range of two col_name NOT BETWEEN 1 AND 10
values (inclusive)
IN (...) Number exists in a list col_name IN (2, 4, 6)
DQL | Applying Constraints

Most Common Logical Operators

Logical Operator Explanation Example


=, !=, < <=, >, >= Standard numerical operators col_name != 4
BETWEEN ... AND ... Number is within range of two values col_name BETWEEN 1.5 AND 10.5
(inclusive)
NOT BETWEEN ... AND ... Number is not within range of two col_name NOT BETWEEN 1 AND 10
values (inclusive)
IN (...) Number exists in a list col_name IN (2, 4, 6)
NOT IN (...) Number does not exist in a list col_name NOT IN (1, 3, 5)
DQL | Applying Constraints

Most Common Logical Operators

Logical Operator Explanation Example


=, !=, < <=, >, >= Standard numerical operators col_name != 4
BETWEEN ... AND ... Number is within range of two values col_name BETWEEN 1.5 AND 10.5
(inclusive)
NOT BETWEEN ... AND ... Number is not within range of two col_name NOT BETWEEN 1 AND 10
values (inclusive)
IN (...) Number exists in a list col_name IN (2, 4, 6)
NOT IN (...) Number does not exist in a list col_name NOT IN (1, 3, 5)
LIKE Used to search for a specified pattern WHERE CustomerName LIKE 'a%' (Finds any
string in a column value that start with "a")

WHERE CustomerName LIKE '_r%' (Finds any


value that have "r" in the second position)
DQL | Applying Constraints

String Comparison Functions

Comparison Operator Explanation Example


= Case sensitive exact comparison. col_name = "abc"
DQL | Applying Constraints

String Comparison Functions

Comparison Operator Explanation Example


= Case sensitive exact comparison. col_name = "abc"
LIKE Case insensitive exact comparison. col_name LIKE "aBc"
DQL | Applying Constraints

String Comparison Functions

Comparison Operator Explanation Example


= Case sensitive exact comparison. col_name = "abc"
LIKE Case insensitive exact comparison. col_name LIKE "aBc"
!= or <> Case sensitive exact string inequality comparison. col_name != "abc"
DQL | Applying Constraints

String Comparison Functions

Comparison Operator Explanation Example


= Case sensitive exact comparison. col_name = "abc"
LIKE Case insensitive exact comparison. col_name LIKE "aBc"
!= or <> Case sensitive exact string inequality comparison. col_name != "abc"
NOT LIKE Case insensitive exact string inequality col_name NOT LIKE "aBc"
comparison.
DQL | Applying Constraints

String Comparison Functions

Comparison Operator Explanation Example


= Case sensitive exact comparison. col_name = "abc"
LIKE Case insensitive exact comparison. col_name LIKE "aBc"
!= or <> Case sensitive exact string inequality comparison. col_name != "abc"
NOT LIKE Case insensitive exact string inequality col_name NOT LIKE "aBc"
comparison.
% Used anywhere in a string to match a sequence of col_name LIKE "%AT%"
zero or more characters (only with LIKE or NOT
LIKE). (matches "AT", "ATTIC", "CAT" or even
"BATS")
DQL | Applying Constraints

String Comparison Functions

Comparison Operator Explanation Example


= Case sensitive exact comparison. col_name = "abc"
LIKE Case insensitive exact comparison. col_name LIKE "aBc"
!= or <> Case sensitive exact string inequality comparison. col_name != "abc"
NOT LIKE Case insensitive exact string inequality col_name NOT LIKE "aBc"
comparison.
% Used anywhere in a string to match a sequence of col_name LIKE "%AT%"
zero or more characters (only with LIKE or NOT
LIKE). (matches "AT", "ATTIC", "CAT" or even
"BATS")
- Used anywhere in a string to match a single col_name LIKE "AN_"
character (only with LIKE or NOT LIKE).
(matches "AND", but not "AN")
DQL | Applying Constraints

String Comparison Functions

Comparison Operator Explanation Example


= Case sensitive exact comparison. col_name = "abc"
LIKE Case insensitive exact comparison. col_name LIKE "aBc"
!= or <> Case sensitive exact string inequality comparison. col_name != "abc"
NOT LIKE Case insensitive exact string inequality col_name NOT LIKE "aBc"
comparison.
% Used anywhere in a string to match a sequence of col_name LIKE "%AT%"
zero or more characters (only with LIKE or NOT
LIKE). (matches "AT", "ATTIC", "CAT" or even
"BATS")
- Used anywhere in a string to match a single col_name LIKE "AN_"
character (only with LIKE or NOT LIKE).
(matches "AND", but not "AN")
IN(...) String exists in a list. col_name IN ("A", "B", "C")
DQL | Applying Constraints

String Comparison Functions

Comparison Operator Explanation Example


= Case sensitive exact comparison. col_name = "abc"
LIKE Case insensitive exact comparison. col_name LIKE "aBc"
!= or <> Case sensitive exact string inequality comparison. col_name != "abc"
NOT LIKE Case insensitive exact string inequality col_name NOT LIKE "aBc"
comparison.
% Used anywhere in a string to match a sequence of col_name LIKE "%AT%"
zero or more characters (only with LIKE or NOT
LIKE) (matches "AT", "ATTIC", "CAT" or even
"BATS")
- Used anywhere in a string to match a single col_name LIKE "AN_"
character (only with LIKE or NOT LIKE)
(matches "AND", but not "AN")
IN(...) String exists in a list. col_name IN ("A", "B", "C")
NOT IN(...) String does not exist in a list. col_name NOT IN ("D", "E", "F")
Formatting Result Set
DQL | Formatting Result Set

The DISTINCT Clause

This clause, when applied over a column name, filters out those tuples which have duplicate values in the specified
column. Student

SELECT DISTINCT column_one, column_two, ... std_id name marks dept_name


FROM table_name 101 Tom 27 CS
WHERE condition_one AND/OR condition_two; 102 Alice 35 IT
SELECT DISTINCT name, marks 103 Mathew 39 ME
FROM Student
104 Barry 42 CS
WHERE (dept_name = 'IT' OR dept_name = 'MCA') AND
(marks BETWEEN 42 AND 50) AND (name NOT LIKE 'a%’); 105 Jane 37 IT

Results 106 John 43 MCA

name marks 107 Maria 45 IT

John 43 108 John 42 IT

Maria 45
John 42
DQL | Formatting Result Set

The ORDER BY Clause

ORDER BY, when applied over column(s), sorts the result set in ascending (default) or descending order.

SELECT column_one, column_two, ...


FROM table_name
WHERE condition_one AND/OR condition_two ...
ORDER BY column_one, column_two, ... ASC/DESC;

SELECT column_one, column_two, ...


FROM table_name
WHERE condition_one AND/OR condition_two ...
ORDER BY column_one, column_two, ...;
DQL | Formatting Result Set

The ORDER BY Clause Student


std_id name marks dept_name
101 Tom 27 CS
SELECT * FROM Student
WHERE (dept_name = 'IT' OR dept_name = 102 Alice 35 IT
'MCA') AND(marks BETWEEN 42 AND 50) AND 103 Mathew 39 ME
(name NOT LIKE 'a%')
104 Barry 42 CS
ORDER BY dept_name, marks;
105 Jane 37 IT
106 John 42 MCA
Results
107 Maria 45 IT
std_id name marks dept_name
108 John 42 IT
108 John 42 IT
107 Maria 45 IT
106 John 42 MCA
DQL | Formatting Result Set

The LIMIT and OFFSET Clause Student


std_id name marks dept_name
SELECT column_one, column_two, ...
101 Tom 27 CS
FROM table_name
WHERE condition_one AND/OR condition_two ... 102 Alice 35 IT
ORDER BY column_one, column_two, ... 103 Mathew 39 ME
LIMIT num_limit, OFFSET num_offset;
104 Barry 42 CS
105 Jane 37 IT
SELECT * FROM Student
WHERE (dept_name = 'IT' OR dept_name = 'MCA') AND 106 John 42 MCA
(marks BETWEEN 42 AND 50) AND (name NOT LIKE 107 Maria 45 IT
'a%')
ORDER BY dept_name, marks 108 John 42 IT
LIMIT 3 OFFSET 2;

What is the output of the given query ? Results


std_id name marks dept_name
106 John 42 MCA
Set Operations
DQL | Set Operations

Set operators are used to combine result sets from two or more than two select statements. The general format of this
command is given below.

result_set_1 SET_OP_1 result_set_2 SET_OP_2 result_set_3 ...

Where SET_OP are the set operators and result_set are the result sets obtained from other queries.
DQL | Set Operations

UNION

The UNION clause combines the results of two queries into a single result set. Any duplicate records are automatically
removed unless UNION ALL is used.

Consider the below given two tables.


Parts_2021 Parts_2020
prts_id prts_color prts_name prts_price prts_id prts_color prts_name prts_price
202 red p2 20 200 red p1 10

203 green p4 25 201 green p2 20


202 red p2 20

SELECT * FROM Parts_2020 Result_Set


UNION prts_id prts_color prts_name prts_price
SELECT * FROM Parts_2021;
200 red p1 10
201 green p2 20
202 red p2 20
203 green p4 25
DQL | Set Operations

UNION ALL

UNION ALL combines the results of two queries into a single result set including the duplicate records.

Consider the below given two tables.


Parts_2021 Parts_2020
prts_id prts_color prts_name prts_price prts_id prts_color prts_name prts_price

202 red p2 20 200 red p1 10

203 green p4 25 201 green p2 20


202 red p2 20

Result_Set
SELECT * FROM Parts_2020 prts_id prts_color prts_name prts_price
UNION ALL 200 red p1 10
SELECT * FROM Parts_2021;
201 green p2 20
202 red p2 20
202 red p2 20
203 green p4 25
DQL | Set Operations

INTERSECT

The INTERSECT operator takes the result sets of two queries and returns only rows that appear in both the result sets.

The INTERSECT operator removes duplicate rows from the final result set.

The INTERSECT ALL operator works in the same way, except it does not remove duplicate rows from the final result set.
DQL | Set Operations
INTERSECT Orders Result_Set
order_id order_name quantity order_id order_name quantity
SELECT *
FROM Orders 201 p2 20 203 p6 50
WHERE quantity BETWEEN 1 AND 100 204 p8 75
202 p4 25
INTERSECT 203 p6 50 208 p16 80

204 p8 75
SELECT *
FROM Orders 205 p10 102
WHERE quantity BETWEEN 50 AND 200; 206 p12 125
207 p14 170
208 p16 80
209 p18 270
210 p20 210
DQL | Set Operations
INTERSECT Orders Result_Set
order_id order_name quantity order_id order_name quantity
SELECT *
FROM Orders 201 p2 20 203 p6 50
WHERE quantity BETWEEN 1 AND 100 204 p8 75
202 p4 25
INTERSECT ALL 203 p6 50 208 p16 80

204 p8 75 203 p6 50
SELECT * 204 p8 75
FROM Orders 205 p10 102
WHERE quantity BETWEEN 50 AND 200; 206 p12 125 208 p16 80

207 p14 170


208 p16 80
209 p18 270
210 p20 210
DQL | Set Operations

EXCEPT

The EXCEPT operator returns those tuples from the first result set that do not appear in the second result set.

The EXCEPT ALL operator works in the same way, except it does not remove duplicates.
DQL | Set Operations
EXCEPT Orders Result_Set

SELECT * order_id order_name quantity order_id order_name quantity


FROM Orders 201 p2 20 201 p2 20
WHERE Quantity BETWEEN 1 AND 100 202 p4 25
202 p4 25
EXCEPT 203 p6 50
204 p8 75
SELECT *
FROM Orders 205 p10 102
WHERE Quantity BETWEEN 50 AND 75; 206 p12 125
207 p14 170
208 p16 80
209 p18 270
210 p20 210

EXCEPT query returns all rows from the Orders table where Quantity is between 1 and 49 or in between 76 and 100
Data from Multiple Tables
DQL | Data from Multiple Tables

The General Structure of Query Revisited

SELECT column_one, column_two, ...


FROM table_name
WHERE condition_one AND/OR condition_two ...
ORDER BY column_one, column_two, ...
LIMIT num_limit, OFFSET num_offset;
Join
SQL | Joins | Simple Join

The JOIN clause creates a virtual table by combining two tables, from which data can be picked. The rule to combine the
two tables is specified through the ON clause.

P Q
A B C D D E F
11 12 80 31 12 13 23
41 23 92 15 31 16 14
12 16 67 29 15 12 19
SQL | Joins | Cross Join

Following is the general format of the CROSS JOIN statement.

SELECT column_one, column_two, ...


FROM table_one CROSS JOIN table_two
WHERE condition_one AND/OR condition_two ...
ORDER BY column_one, column_two, ...
LIMIT num_limit, OFFSET num_offset;

CROSS JOIN does not require the ON clause.

The virtual table created by the CROSS JOIN is the Cartesian product of each row of both the tables.

This join copies all columns from both the tables.


SQL | Joins | Cross Join
SELECT * P Q
FROM P CROSS JOIN Q A B C D D E F
WHERE P.B = Q.E AND P.C = 67 AND Q.F < 20;
11 12 80 31 12 13 23
Virtual _Table
41 23 92 15 31 16 14
P.A P.B P.C P.D Q.D Q.E Q.F
12 16 67 29 15 12 19
11 12 80 31 12 13 23
11 12 80 31 31 16 14
11 12 80 31 15 12 19
41 23 92 15 12 13 23
41 23 92 15 31 16 14
41 23 92 15 15 12 19
12 16 67 29 12 13 23
12 16 67 29 31 16 14
12 16 67 29 15 12 19
SQL | Joins | Cross Join
SELECT * P Q
FROM P CROSS JOIN Q A B C D D E F
WHERE P.B = Q.E AND P.C = 67 AND Q.F < 20;
11 12 80 31 12 13 23
Virtual _Table
41 23 92 15 31 16 14
P.A P.B P.C P.D Q.D Q.E Q.F
12 16 67 29 15 12 19
11 12 80 31 12 13 23
11 12 80 31 31 16 14
Result _Set
11 12 80 31 15 12 19
P.A P.B P.C P.D Q.D Q.E Q.F
41 23 92 15 12 13 23
12 16 67 29 31 16 14
41 23 92 15 31 16 14
41 23 92 15 15 12 19
12 16 67 29 12 13 23
12 16 67 29 31 16 14
12 16 67 29 15 12 19
SQL | Joins | Cartesian Join

If we replace the CROSS JOIN clause with a simple comma it becomes a CARTESIAN JOIN. Every other aspect of this JOIN
is the same as the CROSS JOIN.

The general format of application is reproduced for clarity.

SELECT column_one, column_two, ...


FROM table_one, table_two
WHERE condition_one AND/OR condition_two ...
ORDER BY column_one, column_two, ...
LIMIT num_limit, OFFSET num_offset;
SQL | Joins | Natural Join

Following is the general format of the statement.

SELECT column_one, column_two, ...


FROM table_one NATURAL JOIN table_two
WHERE condition_one AND/OR condition_two ...
ORDER BY column_one, column_two, ...
LIMIT num_limit, OFFSET num_offset;
SQL | Joins | Natural Join

SELECT * P Q
FROM P NATURAL JOIN Q A B C D D E F
WHERE P.C > 80 AND Q.F < 20;
11 12 80 31 12 13 23
41 23 92 15 31 16 14
12 16 67 29 15 12 19

Virtual_Table
P.A P.B P.C D Q.E Q.F
11 12 80 31 16 14
41 23 92 15 12 19
SQL | Joins | Natural Join

SELECT * P Q
FROM P NATURAL JOIN Q A B C D D E F
WHERE P.C > 80 AND Q.F < 20;
11 12 80 31 12 13 23
41 23 92 15 31 16 14
12 16 67 29 15 12 19

Virtual_Table Result_Set
P.A P.B P.C D Q.E Q.F P.A P.B P.C D Q.E Q.F
11 12 80 31 16 14 41 23 92 15 12 19
41 23 92 15 12 19
SQL | Joins | Left Join

Following is the general format of the statement.

SELECT column_one, column_two, ...


FROM table_one LEFT JOIN table_two
ON table_one.col_one = table_two.col_one
WHERE condition_one AND/OR condition_two ...
ORDER BY column_one, column_two, ...
LIMIT num_limit, OFFSET num_offset;
SQL | Joins | Left Join

SELECT * P Q
FROM P LEFT JOIN Q A B C D D E F
ON P.D = Q.D
WHERE P.C > 79 AND Q.F < 20; 11 12 80 31 12 13 23
41 23 92 15 31 16 14
12 16 67 29 15 12 19

Virtual_Table
P.A P.B P.C P.D Q.D Q.E Q.F
11 12 80 31 31 16 14
41 23 92 15 15 12 19
12 16 67 29 NULL NULL NULL
SQL | Joins | Left Join

SELECT * P Q
FROM P LEFT JOIN Q A B C D D E F
ON P.D = Q.D
WHERE P.C > 79 AND Q.F < 20; 11 12 80 31 12 13 23
41 23 92 15 31 16 14
12 16 67 29 15 12 19

Virtual_Table Result_Set
P.A P.B P.C P.D Q.D Q.E Q.F P.A P.B P.C P.D Q.D Q.E Q.F
11 12 80 31 31 16 14 11 12 80 31 31 16 14
41 23 92 15 15 12 19 41 23 92 15 15 12 19
12 16 67 29 NULL NULL NULL
SQL | Joins | Right Join

Following is the general format of the statement.

SELECT column_one, column_two, ...


FROM table_one RIGHT JOIN table_one
ON table_one.col_one = table_two.col_two
WHERE condition_one AND/OR condition_two ...
ORDER BY column_one, column_two, ...
LIMIT num_limit, OFFSET num_offset;
SQL | Joins | Right Join

SELECT *
P Q
FROM P RIGHT JOIN Q
ON P.D = Q.D A B C D D E F
WHERE P.C > 79 AND Q.F < 20;
11 12 80 31 12 13 23
41 23 92 15 31 16 14
12 16 67 29 15 12 19

Virtual_Table
P.A P.B P.C P.D Q.D Q.E Q.F
11 12 80 31 31 16 14
41 23 92 15 15 12 19
NULL NULL NULL NULL 12 13 23
SQL | Joins | Right Join

SELECT *
P Q
FROM P RIGHT JOIN Q
ON P.D = Q.D A B C D D E F
WHERE P.C > 79 AND Q.F < 20;
11 12 80 31 12 13 23
41 23 92 15 31 16 14
12 16 67 29 15 12 19

Virtual_Table Result_Set
P.A P.B P.C P.D Q.D Q.E Q.F P.A P.B P.C P.D Q.D Q.E Q.F
11 12 80 31 31 16 14 11 12 80 31 31 16 14
41 23 92 15 15 12 19 41 23 92 15 15 12 19
NULL NULL NULL NULL 12 13 23
SQL | Joins | Full Join or Full Outer Join

Following is the general format of the statement.

SELECT column_one, column_two, ...


FROM table_one FULL JOIN table_two
ON table_one.col_name = table_two.col_name
WHERE condition_one AND/OR condition_two ...
ORDER BY column_one, column_two, ...
LIMIT num_limit, OFFSET num_offset;
SQL | Joins | Full Join or Full Outer Join

P Q
SELECT *
FROM P FULL JOIN Q A B C D D E F
ON P.D = Q.D 11 12 80 31 12 13 23
WHERE P.C > 79 AND Q.F < 20;
41 23 92 15 31 16 14
12 16 67 29 15 12 19

Virtual_Table
P.A P.B P.C P.D Q.D Q.E Q.F
11 12 80 31 31 16 14
41 23 92 15 15 12 19
12 16 67 29 NULL NULL NULL
NULL NULL NULL NULL 12 13 23
SQL | Joins | Full Join or Full Outer Join

P Q
SELECT *
FROM P FULL JOIN Q A B C D D E F
ON P.D = Q.D 11 12 80 31 12 13 23
WHERE P.C > 79 AND Q.F < 20;
41 23 92 15 31 16 14
12 16 67 29 15 12 19

Virtual_Table Result_Set
P.A P.B P.C P.D Q.D Q.E Q.F P.A P.B P.C P.D Q.D Q.E Q.F
11 12 80 31 31 16 14 11 12 80 31 31 16 14
41 23 92 15 15 12 19 41 23 92 15 15 12 19
12 16 67 29 NULL NULL NULL
NULL NULL NULL NULL 12 13 23
JOINS | Joins| Compound Join

Multiple JOIN statements can be clubbed to create compound JOIN statements. Following points should be noted about
such compound JOIN statements.

SELECT col_name(s)
FROM tab_1 JOIN_TYPE tab_2 JOIN_TYPE tab_3 ....
ON/WHERE cond_1 AND/OR cond_2 ...
ORDER BY col_name(s)
LIMIT num_limit, OFFSET num_offset;

1. Virtual table is created by processing the JOIN clauses from left to right.
FROM tab_1 JOIN_TYPE tab_2 JOIN_TYPE tab_3 ....

2. Multiple conditions can be specified with ON/WHERE clauses. The rows, which do not satisfy the given conditions are
dropped from the virtual table.
ON/WHERE cond_1 AND/OR cond_2 ...

3. ON and WHERE are interchangeable, not just in this context but elsewhere too.
ON/WHERE cond_1 AND/OR cond_2 ...
JOINS | Joins| Compound Join

Supplier Table Parts Table Catalog Table


supp_id supp_name prts_id prts_color prts_name prts_price supp_id prts_id
101 Sunil 200 red p1 10 101 200
102 Ravi 201 green p2 20 101 201
103 Shyam 202 red p2 20 102 203
104 Arya 203 green p4 25 102 201
103 201
104 202

SELECT supp_name, prts_name


FROM Catalog C1 INNER JOIN Supplier S1 INNER Result_Set
JOIN Parts P1
supp_name parts_name
ON C1.supp_id = S1.supp_id AND P1.prts_id =
C1.prts_id AND P1.prts_color = 'red' Sunil p1
Arya p2
Aliasing
SQL | Aliasing

For the sake of readability of complicated queries, column and table names can be given short aliases using the AS
clause (simple whitespace will also work).

Aliases are temporary and exist only for the duration of the query.

SELECT column_one AS alias_one, column_two AS alias_two, ...


FROM table_one AS alias_three FULL JOIN table_two AS alias_four
ON table_one.col_name = table_two.col_name
WHERE condition_one AND/OR condition_two ...
ORDER BY column_one, column_two, ...
LIMIT num_limit, OFFSET num_offset;

SELECT column_one alias_one, column_two alias_two, ...


FROM table_one alias_three FULL JOIN table_two alias_four
ON table_one.col_name = table_two.col_name
WHERE condition_one AND/OR condition_two ...
ORDER BY column_one, column_two, ...
LIMIT num_limit, OFFSET num_offset;
Aggregate Functions
SQL | Aggregate Functions

Aggregate functions calculate aggregate values, like average, maximum and minimum, for a given column.

SELECT AGG_FUN(column_name)
FROM table_name
WHERE condition_one AND/OR condition_two ...;

The above statement will apply the AGG_FUN over the column, titled column_name and will return a single value.
SQL | Aggregate Functions

Function Description
COUNT(*) Counts the number of rows, in the group.
SQL | Aggregate Functions

Function Description
COUNT(*) Counts the number of rows, in the group.

Finds the smallest numerical value in the


MIN(column) specified column for all rows in the group.
SQL | Aggregate Functions

Function Description
COUNT(*) Counts the number of rows, in the group.

Finds the smallest numerical value in the


MIN(column) specified column for all rows in the group.
Finds the largest numerical value in the specified
MAX(column) column for all rows in the group.
SQL | Aggregate Functions

Function Description
COUNT(*) Counts the number of rows, in the group.

Finds the smallest numerical value in the


MIN(column) specified column for all rows in the group.
Finds the largest numerical value in the specified
MAX(column) column for all rows in the group.
Finds the average numerical value in the
AVG(column) specified column for all rows in the group.
SQL | Aggregate Functions

Function Description
COUNT(*) Counts the number of rows, in the group.

Finds the smallest numerical value in the


MIN(column) specified column for all rows in the group.
Finds the largest numerical value in the specified
MAX(column) column for all rows in the group.
Finds the average numerical value in the
AVG(column) specified column for all rows in the group.
Finds the sum of all numerical values in the
SUM(column) specified column for the rows in the group.
Grouped Aggregate Functions
SQL | Grouped Aggregate Functions

Instead of applying the aggregate functions to an entire column we can apply the aggregate functions to groups of data.
GROUP BY clause is used to group the data for the said purpose.

SELECT AGG_FUN(column_name_one)
FROM table_name
WHERE condition_one AND/OR condition_two ...
GROUP BY column_name_two
LIMIT num_limit, OFFSET num_offset;
SQL | Grouped Aggregate Functions
Query Execution Illustration

P SELECT SUM(P.A)
A B C D FROM P
2 7 9 12 WHERE P.D < 50 AND P.C < 40
GROUP BY P.B
1 7 55 80
LIMIT 2 OFFSET 2;
18 13 18 16
20 13 15 60
29 36 33 29
27 36 42 22
6 13 13 90
30 36 22 87
12 18 11 09
13 18 23 07
23 7 18 16
19 13 26 29
SQL | Grouped Aggregate Functions
Query Execution Illustration
First Step: FROM Gets Executed
P SELECT SUM(P.A) P
A B C D FROM P A B C D

2 7 9 12 WHERE P.D < 50 AND P.C < 40 2 7 9 12


GROUP BY P.B 80
1 7 55 80 1 7 55
LIMIT 2 OFFSET 2;
18 13 18 16 18 13 18 16

20 13 15 60 20 13 15 60

29 36 33 29 29 36 33 29

27 36 42 22 27 36 42 22

6 13 13 90 6 13 13 90

30 36 22 87 30 36 22 87

12 18 11 09 12 18 11 09

13 18 23 07 13 18 23 07

23 7 18 16 23 7 18 16

19 13 26 29 19 13 26 29
SQL | Grouped Aggregate Functions
Query Execution Illustration
Second Step: WHERE Gets Executed
P SELECT SUM(P.A) Virtual _Table_1
A B C D FROM P A B C D
2 7 9 12 WHERE P.D < 50 AND P.C < 40 2 7 9 12
GROUP BY P.B
1 7 55 80 18 13 18 16
LIMIT 2 OFFSET 2;
18 13 18 16 29 36 33 29
20 13 15 60 12 18 11 09
29 36 33 29 13 18 23 07
27 36 42 22 23 7 18 16
6 13 13 90 19 13 26 29
30 36 22 87
12 18 11 09
13 18 23 07
23 7 18 16
19 13 26 29
SQL | Grouped Aggregate Functions
Query Execution Illustration
Third Step: GROUP BY Gets Executed

Virtual _Table_1 SELECT SUM(P.A) Virtual _Table_2


FROM P
A B C D WHERE P.D < 50 AND P.C < 40 A B C D
2 7 9 12 GROUP BY P.B 2 7 9 12
LIMIT 2 OFFSET 2;
18 13 18 16 23 7 18 16
29 36 33 29 18 13 18 16
12 18 11 09 19 13 26 29
13 18 23 07 29 36 33 29
23 7 18 16 12 18 11 09
19 13 26 29 13 18 23 07
SQL | Grouped Aggregate Functions
Query Execution Illustration
Fourth Step: SUM(P.A) Gets Executed

Virtual _Table_2 SELECT SUM(P.A) Virtual _Table_3


FROM P
A B C D WHERE P.D < 50 AND P.C < 40 SUM(P.A)
2 7 9 12 GROUP BY P.B 25
LIMIT 2 OFFSET 2;
23 7 18 16 37
18 13 18 16 25
19 13 26 29 29
12 18 11 09
13 18 23 07
29 36 33 29
SQL | Grouped Aggregate Functions
Query Execution Illustration
Fifth Step: LIMIT and OFFSET Gets Executed

Virtual _Table_3 SELECT SUM(P.A) Result_Set


FROM P
SUM(P.A) WHERE P.D < 50 AND P.C < 40 SUM(P.A)
25 GROUP BY P.B 25
LIMIT 2 OFFSET 2;
37 29
25
29
Further Filtering the Groups
SQL | Further Filtering the Groups

The GROUP BY clause is executed after the WHERE clause (which filters the rows which are to be grouped). HAVING
clause is used to further filter the grouped data.

The HAVING clause constraints are written the same way as the WHERE clause constraints, but are applied to the
grouped rows.

SELECT AGG_FUN(column_name_1)
FROM table_name
WHERE condition_1 AND/OR condition_2 ...
GROUP BY column_name_2
HAVING condition_3 and/or condition_4 ...
LIMIT num_limit, OFFSET num_offset;
SQL | Further Filtering the Groups
Query Execution Illustration

P
SELECT SUM(P.A)
A B C D
FROM P
2 7 9 12 WHERE P.D < 50 AND P.C < 40
1 7 55 80 GROUP BY P.B
18 13 18 16
HAVING P.B > 7
LIMIT 2 OFFSET 2;
20 13 15 60
29 36 33 29
27 36 42 22
6 13 13 90
30 36 22 87
12 18 11 09
13 18 23 07
23 7 18 16
19 13 26 29
SQL | Further Filtering the Groups
Query Execution Illustration

P Virtual _Table_2
SELECT SUM(P.A)
A B C D
FROM P A B C D
2 7 9 12 WHERE P.D < 50 AND P.C < 40 2 7 9 12
1 7 55 80 GROUP BY P.B 23 7 18 16
18 13 18 16
HAVING P.B > 7
LIMIT 2 OFFSET 2; 18 13 18 16
20 13 15 60
19 13 26 29
29 36 33 29
29 36 33 29
27 36 42 22
12 18 11 09
6 13 13 90
13 18 23 07
30 36 22 87
12 18 11 09
13 18 23 07
23 7 18 16
19 13 26 29
SQL | Further Filtering the Groups
Query Execution Illustration
HAVING Gets Executed
Virtual _Table_2 Vitual_Table_3
SELECT SUM(P.A)
A B C D FROM P A B C D
2 7 9 12 WHERE P.D < 50 AND P.C < 40 18 13 18 16
GROUP BY P.B
23 7 18 16 HAVING P.B > 7 19 13 26 29
18 13 18 16 LIMIT 2 OFFSET 2; 29 36 33 29
19 13 26 29 12 18 11 09
29 36 33 29 13 18 23 07
12 18 11 09
13 18 23 07
SQL | Further Filtering the Groups
Query Execution Illustration
SUM(P.A) Gets Executed
Vitual_Table_3 Vitual_Table_4
SELECT SUM(P.A)
A B C D FROM P SUM(P.A)
18 13 18 16 WHERE P.D < 50 AND P.C < 40 37
GROUP BY P.B
19 13 26 29 HAVING P.B > 7 25
12 18 11 09 LIMIT 2 OFFSET 2; 29
13 18 23 07
29 36 33 29
SQL | Further Filtering the Groups
Query Execution Illustration LIMIT and OFFSET Gets Executed

Vitual_Table_4 Result_Set
SELECT SUM(P.A)
SUM(P.A) FROM P SUM(P.A)
WHERE P.D < 50 AND P.C < 40
37 29
GROUP BY P.B
25 HAVING P.B > 7
LIMIT 2 OFFSET 2;
29
Flow of Execution of Query
SQL | Flow of Execution of Query

1. The FROM clause and JOIN clause are first executed to determine the total working set of data.
SQL | Flow of Execution of Query

1. The FROM clause and JOIN clause are first executed to determine the total working set of data.
2. WHERE constraints are applied to the individual rows, and rows that do not satisfy the constraint
are discarded.
SQL | Flow of Execution of Query

1. The FROM clause and JOIN clause are first executed to determine the total working set of data.
2. WHERE constraints are applied to the individual rows, and rows that do not satisfy the constraint
are discarded.
3. The remaining rows after the WHERE constraints are applied are then grouped based on common
values in the column specified in the GROUP BY clause.
SQL | Flow of Execution of Query

1. The FROM clause and JOIN clause are first executed to determine the total working set of data.
2. WHERE constraints are applied to the individual rows, and rows that do not satisfy the constraint
are discarded.
3. The remaining rows after the WHERE constraints are applied are then grouped based on common
values in the column specified in the GROUP BY clause.
4. If the query has a GROUP BY clause, then the constraints in the HAVING clause are then applied to
the grouped rows, which discards the grouped rows that do not satisfy the constraints.
SQL | Flow of Execution of Query

1. The FROM clause and JOIN clause are first executed to determine the total working set of data.
2. WHERE constraints are applied to the individual rows, and rows that do not satisfy the constraint
are discarded.
3. The remaining rows after the WHERE constraints are applied are then grouped based on common
values in the column specified in the GROUP BY clause.
4. If the query has a GROUP BY clause, then the constraints in the HAVING clause are then applied to
the grouped rows, which discards the grouped rows that do not satisfy the constraints.
5. SELECT part of the query is then executed, which selects the specified columns and evaluates
expressions (mentioned with the SELECT clause) or applies the aggregate functions.
SQL | Flow of Execution of Query

1. The FROM clause and JOIN clause are first executed to determine the total working set of data.
2. WHERE constraints are applied to the individual rows, and rows that do not satisfy the constraint
are discarded.
3. The remaining rows after the WHERE constraints are applied are then grouped based on common
values in the column specified in the GROUP BY clause.
4. If the query has a GROUP BY clause, then the constraints in the HAVING clause are then applied to
the grouped rows, which discards the grouped rows that do not satisfy the constraints.
5. SELECT part of the query is then executed, which selects the specified columns and evaluates
expressions (mentioned with the SELECT clause) or applies the aggregate functions.
6. Of the remaining rows, rows with duplicate values in the column marked as DISTINCT will be
discarded.
SQL | Flow of Execution of Query

1. The FROM clause and JOIN clause are first executed to determine the total working set of data.
2. WHERE constraints are applied to the individual rows, and rows that do not satisfy the constraint
are discarded.
3. The remaining rows after the WHERE constraints are applied are then grouped based on common
values in the column specified in the GROUP BY clause.
4. If the query has a GROUP BY clause, then the constraints in the HAVING clause are then applied to
the grouped rows, which discards the grouped rows that do not satisfy the constraints.
5. SELECT part of the query is then executed, which selects the specified columns and evaluates
expressions (mentioned with the SELECT clause) or applies the aggregate functions.
6. Of the remaining rows, rows with duplicate values in the column marked as DISTINCT will be
discarded.
7. If an order is specified by the ORDER BY clause, the rows are then sorted by the specified data in
either ascending or descending order.
SQL | Flow of Execution of Query

1. The FROM clause and JOIN clause are first executed to determine the total working set of data.
2. WHERE constraints are applied to the individual rows, and rows that do not satisfy the constraint
are discarded.
3. The remaining rows after the WHERE constraints are applied are then grouped based on common
values in the column specified in the GROUP BY clause.
4. If the query has a GROUP BY clause, then the constraints in the HAVING clause are then applied to
the grouped rows, which discards the grouped rows that do not satisfy the constraints.
5. SELECT part of the query is then executed, which selects the specified columns and evaluates
expressions (mentioned with the SELECT clause) or applies the aggregate functions.
6. Of the remaining rows, rows with duplicate values in the column marked as DISTINCT will be
discarded.
7. If an order is specified by the ORDER BY clause, the rows are then sorted by the specified data in
either ascending or descending order.
8. Finally, the rows that fall outside the range specified by the LIMIT and OFFSET are discarded,
leaving the final set of rows to be returned from the query.
Nested Query
SQL | Nested Queries

A Nested Query (also called subquery or inner query) is a query embedded within another query. The nested query may
appear in SELECT, FROM or WHERE clauses.

Nested queries result in a virtual table. These virtual tables can be referenced from anywhere a normal table can be
referenced from.

Nested queries can be broadly categorized into the following two categories.
1. When the nested queries are independent of each other
2. When the nested queries depend on each other.
SQL | Nested Queries

INDEPENDENT QUERY

Supplier Table Parts Table Catalog Table


supp_id supp_name prts_id prts_color prts_name prts_price supp_id prts_id
101 Sunil 200 red p1 10 101 200
102 Ravi 201 green p2 20 101 201
103 Shyam 202 red p2 20 102 203
104 Arya 203 green p4 25 102 201
103 201
104 202
SELECT supp_id
FROM Catalog
WHERE prts_id IN (SELECT prts_id
FROM Parts
WHERE prts_color = 'Red');
SQL | Nested Queries

Supplier Table Parts Table Catalog Table


supp_id supp_name prts_id prts_color prts_name prts_price supp_id prts_id
101 Sunil 200 red p1 10 101 200
102 Ravi 201 green p2 20 101 201
103 Shyam 202 red p2 20 102 203
104 Arya 203 green p4 25 102 201
103 201
104 202
FROM Gets Executed
Catalog
SELECT supp_id
supp_id prts_id
FROM Catalog
WHERE prts_id IN (SELECT prts_id 101 200
FROM Parts 101 201
WHERE prts_color = 'Red'); 102 203
102 201
103 201
104 202
SQL | Nested Queries

Supplier Table Parts Table Catalog Table


supp_id supp_name prts_id prts_color prts_name prts_price supp_id prts_id
101 Sunil 200 red p1 10 101 200
102 Ravi 201 green p2 20 101 201
103 Shyam 202 red p2 20 102 203
104 Arya 203 green p4 25 102 201
103 201
104 202
WHERE Gets Executed
Catalog
SELECT supp_id
supp_id prts_id
FROM Catalog
WHERE prts_id IN (SELECT prts_id 101 200
FROM Parts 101 201
WHERE prts_color = 'Red'); 102 203
102 201
103 201
104 202
SQL | Nested Queries

Supplier Table Parts Table Catalog Table


supp_id supp_name prts_id prts_color prts_name prts_price supp_id prts_id
101 Sunil 200 red p1 10 101 200
102 Ravi 201 green p2 20 101 201
103 Shyam 202 red p2 20 102 203
104 Arya 203 green p4 25 102 201
103 201
104 202
Nested Query Gets Executed

SELECT supp_id
FROM Catalog Virtual_Table_1
WHERE prts_id IN (SELECT prts_id
prts_id
FROM Parts
WHERE prts_color = 'Red'); 200
202
SQL | Nested Queries

Supplier Table Parts Table Catalog Table


supp_id supp_name prts_id prts_color prts_name prts_price supp_id prts_id
101 Sunil 200 red p1 10 101 200
102 Ravi 201 green p2 20 101 201
103 Shyam 202 red p2 20 102 203
104 Arya 203 green p4 25 102 201
103 201
104 202
Nested Query Gets Executed

SELECT supp_id
FROM Catalog Virtual_Table_1
WHERE prts_id IN Virtual_Table_1;
prts_id
200
202
SQL | Nested Queries

Supplier Table Parts Table Catalog Table


Virtual_Table_1
supp_id supp_name prts_id prts_color prts_name prts_price supp_id prts_id
101 Sunil 200 red p1 10 101 200 prts_id
102 Ravi 201 green p2 20 101 201 200
103 Shyam 202 red p2 20 102 203
202
104 Arya 203 green p4 25 102 201
103 201
104 202
SELECT Gets Executed

SELECT supp_id
FROM Catalog
WHERE prts_id IN Virtual_Table_1;
SQL | Nested Queries

Supplier Table Parts Table Catalog Table


supp_id supp_name prts_id prts_color prts_name prts_price supp_id prts_id
101 Sunil 200 red p1 10 101 200
102 Ravi 201 green p2 20 101 201
103 Shyam 202 red p2 20 102 203
104 Arya 203 green p4 25 102 201
103 201
104 202
SELECT Gets Executed
Catalog
Virtual_Table_1
SELECT supp_id supp_id prts_id
FROM Catalog prts_id
WHERE prts_id IN Virtual_Table_1; 101 200
200
101 201
202
102 203
102 201
103 201
104 202
SQL | Nested Queries

Supplier Table Parts Table Catalog Table


supp_id supp_name prts_id prts_color prts_name prts_price supp_id prts_id
101 Sunil 200 red p1 10 101 200
102 Ravi 201 green p2 20 101 201
103 Shyam 202 red p2 20 102 203
104 Arya 203 green p4 25 102 201
103 201
104 202

SELECT supp_id Result_Set


FROM Catalog
WHERE prts_id IN Virtual_Table_1; supp_id
101
104
SQL | Nested Queries
DEPENDENT QUERY
Parts Table Catalog Table
Supplier Table
prts_id prts_color prts_name prts_price supp_id prts_id
supp_id supp_name
101 Sunil 200 red p1 10 101 200

102 Ravi 201 green p2 20 101 201

103 Shyam 202 red p2 20 102 203

104 Arya 203 green p4 25 102 201


103 201
SELECT supp_id 104 202
FROM Catalog C
WHERE EXISTS (SELECT *
FROM Parts P
WHERE P.prts_id = C.prts_id AND P.prts_color = 'Red');
SQL | Nested Queries
Parts Table Catalog Table
Supplier Table
prts_id prts_color prts_name prts_price supp_id prts_id
supp_id supp_name
101 Sunil 200 red p1 10 101 200

102 Ravi 201 green p2 20 101 201

103 Shyam 202 red p2 20 102 203

104 Arya 203 green p4 25 102 201


103 201
SELECT supp_id 104 202
FROM Catalog C
WHERE EXISTS (SELECT *
FROM Parts P
WHERE P.prts_id = C.prts_id AND P.prts_color = 'Red');
SQL | Nested Queries
Parts Table Catalog Table
Supplier Table
prts_id prts_color prts_name prts_price supp_id prts_id
supp_id supp_name
101 Sunil 200 red p1 10 101 200

102 Ravi 201 green p2 20 101 201

103 Shyam 202 red p2 20 102 203

104 Arya 203 green p4 25 102 201


103 201
FROM Gets Executed 104 202

SELECT supp_id C
FROM Catalog C supp_id prts_id
WHERE EXISTS (SELECT * 101 200
FROM Parts P
101 201
WHERE P.prts_id = C.prts_id AND P.prts_color = 'Red');
102 203
102 201
103 201
104 202
SQL | Nested Queries
Parts Table Catalog Table
Supplier Table
prts_id prts_color prts_name prts_price supp_id prts_id
supp_id supp_name
101 Sunil 200 red p1 10 101 200

102 Ravi 201 green p2 20 101 201

103 Shyam 202 red p2 20 102 203

104 Arya 203 green p4 25 102 201


103 201
WHERE Gets Executed 104 202

SELECT supp_id
C P Virtual _Table
FROM Catalog C
supp_id prts_id prts_id prts_color prts_name prts_price supp_id prts_id
WHERE EXISTS (SELECT *
101 200 200 red p1 10 101 200
FROM Parts P
WHERE P.prts_id = C.prts_id 101 201 201 green p2 20

AND P.prts_color = 'Red'); 102 203 202 red p2 20

102 201 203 green p4 25

103 201

104 202
SQL | Nested Queries
Parts Table Catalog Table
Supplier Table
prts_id prts_color prts_name prts_price supp_id prts_id
supp_id supp_name
101 Sunil 200 red p1 10 101 200

102 Ravi 201 green p2 20 101 201

103 Shyam 202 red p2 20 102 203

104 Arya 203 green p4 25 102 201


103 201
WHERE Gets Executed 104 202

SELECT supp_id
C P Virtual _Table
FROM Catalog C
supp_id prts_id prts_id prts_color prts_name prts_price supp_id prts_id
WHERE EXISTS (SELECT *
101 200 200 red p1 10 101 200
FROM Parts P
101 201 201 green p2 20
WHERE P.prts_id = C.prts_id
AND P.prts_color = 'Red'); 102 203 202 red p2 20

102 201 203 green p4 25


103 201

104 202
SQL | Nested Queries
Parts Table Catalog Table
Supplier Table
prts_id prts_color prts_name prts_price supp_id prts_id
supp_id supp_name
101 Sunil 200 red p1 10 101 200

102 Ravi 201 green p2 20 101 201

103 Shyam 202 red p2 20 102 203

104 Arya 203 green p4 25 102 201


103 201
WHERE Gets Executed 104 202

SELECT supp_id
C P Virtual _Table
FROM Catalog C
supp_id prts_id prts_id prts_color prts_name prts_price supp_id prts_id
WHERE EXISTS (SELECT *
101 200 200 red p1 10 101 200
FROM Parts P
101 201 201 green p2 20 104 202
WHERE P.prts_id = C.prts_id
AND P.prts_color = 'Red'); 102 203 202 red p2 20

102 201 203 green p4 25


103 201

104 202
SQL | Nested Queries
Parts Table Catalog Table
Supplier Table
prts_id prts_color prts_name prts_price supp_id prts_id
supp_id supp_name
101 Sunil 200 red p1 10 101 200

102 Ravi 201 green p2 20 101 201

103 Shyam 202 red p2 20 102 203

104 Arya 203 green p4 25 102 201


103 201
SELECT Gets Executed 104 202

SELECT supp_id Result_Set


FROM Catalog C
WHERE EXISTS (SELECT * supp_id
FROM Parts P 101
WHERE P.prts_id = C.prts_id
AND P.prts_color = 'Red'); 104
DBMS| SQL InfyTQ 2021
Consider the following relational schema:
employee(empId, empName, empDept)
customer(custId, custName, salesRepId, rating)
salesRepId is a foreign key referring to empId of the employee relation.
00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Assume that each employee makes a sale to at least one customer.
What does the following query return?
SELECT empName
FROM employee E
WHERE NOT EXISTS (SELECT custId
FROM customer C
WHERE C.salesRepId = E.empId
AND C.rating = 'GOOD');
Names of all the employees with Names of all the employees
A. at least one of their customers C. with none of their customers
having a 'GOOD' rating. having a 'GOOD' rating.
Names of all the employees with Names of all the employees
B. at most one of their customers D. with all their customers
having a 'GOOD' rating. having a 'GOOD' rating.
DBMS| SQL InfyTQ 2021
Consider the following relational schema:
employee(empId, empName, empDept)
customer(custId, custName, salesRepId, rating)
salesRepId is a foreign key referring to empId of the employee relation.
Assume that each employee makes a sale to at least one customer.
What does the following query return?
SELECT empName
FROM employee E
WHERE NOT EXISTS (SELECT custId
FROM customer C
WHERE C.salesRepId = E.empId
AND C.rating = 'GOOD');
Names of all the employees with Names of all the employees
A. at least one of their customers C. with none of their customers
having a 'GOOD' rating. having a 'GOOD' rating.
Names of all the employees with Names of all the employees
B. at most one of their customers D. with all their customers
having a 'GOOD' rating. having a 'GOOD' rating.
DBMS| SQL
Consider the schema of 3 tables of Customer, Product and Purchase and one
customer can purchase all products:
Customer(customer_id, customer_name)
Product(product_id, product_color)
00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Purchase(customer_id, product_id, price)

SELECT P1.customer_id
FROM Purchase P1
WHERE NOT EXISTS (SELECT product_id
FROM Product
EXCEPT
SELECT product_id
FROM Purchase P2
WHERE P1.customer_id = P2.customer_id);
The given Query will find:
All customer_id who purchased All customer_id who purchased at
A. at least one product. C. least two products.

All customer_id who does not All customer_id who purchased


B. D.
purchase any product. all products.
DBMS| SQL
Consider the schema of 3 tables of Customer, Product and Purchase and one
customer can purchase all products:
Customer(customer_id, customer_name)
Product(product_id, product_color)
Purchase(customer_id, product_id, price)

SELECT P1.customer_id
FROM Purchase P1
WHERE NOT EXISTS (SELECT product_id
FROM Product
EXCEPT
SELECT product_id
FROM Purchase P2
WHERE P1.customer_id = P2.customer_id);
The given Query will find:
All customer_id who purchased All customer_id who purchased at
A. at least one product. C. least two products.

All customer_id who does not All customer_id who purchased


B. D.
purchase any product. all products.
DBMS| SQL
Consider the schema of 3 tables of Customer, Product and Purchase:

Customer(customer_id, customer_name)
Product(product_id, product_color)
00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Purchase(customer_id, product_id, price)
SELECT customer_id
FROM Purchase, Product
WHERE Product.product_id = Purchase.product_id
AND product_color = 'Red'
UNION
SELECT customer_id
FROM Purchase, Product
WHERE Product.product_id = Purchase.product_id
AND product_color = 'Green'
All customer_id who purchase the All customer_id who purchase
A. C.
red color product red or green color products.
All customer_id who do not All customer_id who purchase
B. purchase red and green color D. red and green color products.
products.
DBMS| SQL
Consider the schema of 3 tables of Customer, Product and Purchase:

Customer(customer_id, customer_name)
Product(product_id, product_color)
Purchase(customer_id, product_id, price)
SELECT customer_id
FROM Purchase, Product
WHERE Product.product_id = Purchase.product_id
AND product_color = 'Red'
UNION
SELECT customer_id
FROM Purchase, Product
WHERE Product.product_id = Purchase.product_id
AND product_color = 'Green'
All customer_id who purchase the All customer_id who purchase
A. C.
red color product red or green color products.
All customer_id who do not All customer_id who purchase
B. purchase red and green color D. red and green color products.
products.
DBMS| SQL
Consider the table Staff given below:

staff_id name
Staff

age gender sup_id


00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
101 Tom 27 M 107

102 Alice 35 F 103

103 Mathew 39 M 106

104 Barry 42 M 103

105 Jane 37 F 106

106 John 42 M 107

107 Maria 45 F 107

SELECT *
FROM Staff S1 INNER JOIN Staff S2
ON S1.sup_id = S2.sup_id
WHERE S1.gender <> S2.gender AND S1.age < 40;
How many row(s) will be fetched in the output when the above query is executed ?
A. 3 C. 2
B. 4 D. 5
DBMS| SQL
Consider the table Staff given below:
Staff

staff_id name age gender sup_id

101 Tom 27 M 107

102 Alice 35 F 103

103 Mathew 39 M 106

104 Barry 42 M 103

105 Jane 37 F 106

106 John 42 M 107

107 Maria 45 F 107

SELECT *
FROM Staff S1 INNER JOIN Staff S2
ON S1.sup_id = S2.sup_id
WHERE S1.gender <> S2.gender AND S1.age < 40;
How many row(s) will be fetched in the output when the above query is executed ?
A. 3 C. 2
B. 4 D. 5
DBMS| SQL InfyTQ 2021

00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
The relation book(title, price) contains the titles and prices of
different books. Assuming that no two books have the same price, what
does the following SQL query list?
SELECT title
FROM book as B
WHERE (SELECT count(*)
FROM book as T
WHERE T.price < B.price) < 3

Titles of the three most Title of the three least


A. C.
expensive books. expensive books.
Title of the third most expensive Titles of the third least
B. D.
book. expensive books.
DBMS| SQL InfyTQ 2021
The relation book(title, price) contains the titles and prices of
different books. Assuming that no two books have the same price, what
does the following SQL query list?
SELECT title
FROM book as B
WHERE (SELECT count(*)
FROM book as T
WHERE T.price < B.price) < 3

Titles of the three most Title of the three least


A. C.
expensive books. expensive books.
Title of the third most expensive Titles of the third least
B. D.
book. expensive books.
DBMS| SQL

00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Student

std_id name marks dept_name

101 Tom 27 CS

102 Alice 35 IT

103 Mathew 39 ME

104 Barry 42 CS

105 Jane 37 IT Select those students who have the


A.
106 John 42 MCA lowest marks.

107 Maria 45 IT Select those students' details who


B. have the lowest marks in the
108 John 42 IT department.

What will the following query do ? Select those students' names who
SELECT S1.name C. have the lowest marks in IT the
department.
FROM Student S1
WHERE NOT EXISTS (SELECT * FROM Student S2 WHERE S1.std_id <> Select those students' names who
S2.std_id AND S1.marks > S2.marks AND S1.dept_name = S2.dept_name) D. have the lowest marks in the
department.
DBMS| SQL
Student

std_id name marks dept_name

101 Tom 27 CS

102 Alice 35 IT

103 Mathew 39 ME

104 Barry 42 CS

105 Jane 37 IT Select those students who have the


A.
106 John 42 MCA lowest marks.

107 Maria 45 IT Select those students' details who


B. have the lowest marks in the
108 John 42 IT department.

What will the following query do ? Select those students' names who
SELECT S1.name C. have the lowest marks in IT the
department.
FROM Student S1
WHERE NOT EXISTS (SELECT * FROM Student S2 WHERE S1.std_id <> Select those students' names who
S2.std_id AND S1.marks > S2.marks AND S1.dept_name = S2.dept_name) D. have the lowest marks in the
department.
DBMS| SQL

00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Consider the following two tables taken from some database.
P Q
A B C D D E F
11 12 80 31 12 13 23
41 23 92 15 31 16 14
12 16 67 29 15 12 19

What is the output of the following query?


SELECT SUM(Q.D)
FROM P CROSS JOIN Q
WHERE P.C >= 80
GROUP BY Q.D
LIMIT 2 OFFSET 1;
A. 30, 62, 70 C. 30, 62
B. 24, 30 D. 24, 30, 62
DBMS| SQL

Consider the following two tables taken from some database.


P Q
A B C D D E F
11 12 80 31 12 13 23
41 23 92 15 31 16 14
12 16 67 29 15 12 19

What is the output of the following query?


SELECT SUM(Q.D)
FROM P CROSS JOIN Q
WHERE P.C >= 80
GROUP BY Q.D
LIMIT 2 OFFSET 1;
A. 30, 62, 70 C. 30, 62
B. 24, 30 D. 24, 30, 62
DBMS| SQL
Consider the table bookdetail given below:

book_id pages
Bookdetail

cost purchase_date
00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
101 400 250 1-Jan-19

102 500 400 5-Jun-18

103 300 500 12-Feb-19

104 250 350 31-May-19

105 350 500 12-Dec-18

106 450 400 5-Jun-19

107 600 250 28-Apr-19

What will be the values of pages in output when the above query is executed?
SELECT book_id, pages A. 300, 450, 300, 500, 350, 450
FROM Bookdetail
WHERE cost >= 400 AND purchase_date BETWEEN '01-Jan-19' AND '5-Jun-19' B. 300, 500, 350, 450
UNION ALL
SELECT book_id, pages C. 300, 450, 500, 350, 450
FROM Bookdetail
WHERE pages > 250 AND cost >= 400; D. 300, 300, 500, 350, 450
DBMS| SQL
Consider the table bookdetail given below:
Bookdetail

book_id pages cost purchase_date

101 400 250 1-Jan-19

102 500 400 5-Jun-18

103 300 500 12-Feb-19

104 250 350 31-May-19

105 350 500 12-Dec-18

106 450 400 5-Jun-19

107 600 250 28-Apr-19

What will be the values of pages in output when the above query is executed?
SELECT book_id, pages A. 300, 450, 300, 500, 350, 450
FROM Bookdetail
WHERE cost >= 400 AND purchase_date BETWEEN '01-Jan-19' AND '5-Jun-19' B. 300, 500, 350, 450
UNION ALL
SELECT book_id, pages C. 300, 450, 500, 350, 450
FROM Bookdetail
WHERE pages > 250 AND cost >= 400; D. 300, 300, 500, 350, 450

You might also like