DQL (Data Query Language)
DQL (Data Query Language)
DQL - Introduction
SQL | DQL - Introduction
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
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
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
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
Result_Set
SELECT prts_id prts_id
FROM Parts 200
WHERE prts_color = 'red';
202
DQL | Applying Constraints
Result_Set
SELECT prts_id
FROM Parts prts_id
WHERE prts_color = 'red' AND prts_price < 20; 200
DQL | Applying Constraints
This clause, when applied over a column name, filters out those tuples which have duplicate values in the specified
column. Student
Maria 45
John 42
DQL | Formatting Result Set
ORDER BY, when applied over column(s), sorts the result set in ascending (default) or descending order.
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.
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.
UNION ALL
UNION ALL combines the results of two queries into a single result set including the duplicate records.
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
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
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 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
The virtual table created by the CROSS JOIN is the Cartesian product of each row of both the tables.
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.
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
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
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
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
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.
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.
Function Description
COUNT(*) Counts the number of rows, in the group.
Function Description
COUNT(*) Counts the number of rows, in the group.
Function Description
COUNT(*) Counts the number of rows, in the group.
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
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
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
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
SELECT supp_id
FROM Catalog Virtual_Table_1
WHERE prts_id IN Virtual_Table_1;
prts_id
200
202
SQL | Nested Queries
SELECT supp_id
FROM Catalog
WHERE prts_id IN Virtual_Table_1;
SQL | Nested Queries
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
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
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
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
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
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
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
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.
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.
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
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
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
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
101 Tom 27 CS
102 Alice 35 IT
103 Mathew 39 ME
104 Barry 42 CS
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
101 Tom 27 CS
102 Alice 35 IT
103 Mathew 39 ME
104 Barry 42 CS
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
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
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
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