InfyTQ - SQL Questions
InfyTQ - SQL Questions
TABLE: products
How many rows will be obtained as output after successfully executing the given query?
SELECT pid, customername FROM products WHERE price > 10 GROUP BY pid,
customername HAVING COUNT (DISTINCT brand) > 1;
a) 2
b) 1
c) 4
d) 3
Answer: b
Explanation: There is only one row with a distinct brand whose price is > 1.
2) Consider the table products given above. Choose the suitable query to display product
names ending with the alphabet ‘y’
TABLE: Products
Answer: a
Explanation: %y gives all the selected column values that end in y.
TABLE: Products
P1001 TV 35000
TABLE: Seller
How many rows will be obtained as output after successfully executing the given query?
a) 1
b) 2
c) 3
d) 4
Answer: b
Explanation: The table seller with status =' Delivered' have 3 rows with Productid P1001 and
P1004. Among those Productid, the Price of >=20000 is present for only P1001 (Price = 35000).
So as a result, only 2 rows will be fetched.
Answer: a
Explanation: Like operator in SQL is case insensitive and to search for strings that begins with
‘xyz’ the Like operator ‘xyz%’ is used. So, the first and the third statement are incorrect.
5) For the given Products table, Choose the correct statement to retrieve the values that
contain ‘_ea_’ in the ‘Productname’ column?
Answer: c
Explanation: To output a string that contains ‘_ea_’, the correct syntax is LIKE ‘%ea%’ where
the ‘%’ denotes zero or more characters
b)
Project Average_Salary Total_Bonus
c)
Project Average_Salary Total_Bonus
d)
Project Average_Salary Total_Bonus
Answer: a
Explanation: By the use of Group By statement, the given table is reduced to 3 rows and from
the three rows, only 2 rows meet the given constraints such as (Average of Stipend > 20000
and Sum of Bonus > 500). Further, in the two rows, the ROUND and SUM functions are applied
to get the given output.
a) Both A & B
b) Neither A nor B
c) A
d) B
Answer: c
Explanation: A’s Query selects the required information from the given table and for performing
sorting, the Order By keyword is used, where the Location is sorted in the Ascending order first
and for the rows which have the same location, the stipend is sorted in the descending order.
8) Consider the table Employee given below
Location name Salary
Answer: a
Explanation: The LOWER and UPPER function converts the location and name into lowercase
and uppercase respectively and the Like operator which uses ‘%t’ for location and ‘%n’ for name
matches the pattern with the table and prints the required output.
a) 0
b) 2
c) 1
d) 3
Answer: b
Explanation: Initially the table contains 8 rows. Applying the first condition ( Stipend>25000), it is
reduced to 5 rows and after applying the second condition (LIKE ‘T%’ ) the table is reduced to 3
rows. Then the final condition of the (Bonus - Not Null) is applied and the 3 rows are reduced to
2 rows as one row contains a NULL value. So 2 rows are obtained as a result
Britannia Goodday 25
Britannia Bourbon 20
Cadbury Oreo 30
Sunfeast Nice 30
Answer: b
Explanation: Initially the table contains 6 rows. Applying the first condition (price <=30), the
table is reduced to 5 rows. The next condition is to perform sorting by using the Order By
keyword. So, the brand is sorted in the Ascending order first and for the rows which have the
same brand, the product name is sorted in the descending order
I1 Siemens
I2 CITI
I3 JP Morgan
I4 Lufthansa
TABLE: Employee_allocation
eid name Pid
E1 Aditi I2
E2 Elvin I2
E3 Jaydev I3
E4 Axar NULL
E5 Vijai NULL
SELECT C.pid,C.name,E.eid FROM Company_Projects C FULL JOIN Employee_allocation E
ON C.pid=E.pid AND E.pid IS NOT NULL;
How many rows will be obtained as output after successfully executing the given query?
a) 6
b) 3
c) 7
d) 5
Answer: b
Explanation: The FULL JOIN keyword returns all the matching data from both tables whether
the other table matches or not. So, the output data will be completely dependent on the given
query "C.pid=E.pid AND E.pid IS NOT NULL". The column E.pid has only 3 rows without the
NULL value where the rows E4 and E5 have NULL values. So, only 3 rows will be fetched as a
result
E1 Laptop 30000 8 10
E2 Printer 5000 5 15
E3 Camera 7000 20 18
E5 Smartphones 9000 6 10
E6 TV 25000 30 30
a) 6
b) 0
c) 2
d) 12
Answer: c
Explanation: Initially the table contains 6 rows. After applying SQL Inner Join, the table is
increased to 10 rows as the rows E3 AND E6 have unique Stock values and other rows don't.
After applying the next condition (e1.Discount <> e2.Discount) the table is reduced to 4 rows.
Then the final condition of (e1.Discount>7) is applied and the table is reduced to 2 rows as only
two out of four rows contain a discount value greater than 7. So, only two rows are fetched as
the result.
E1 Laptop 30000 8 10
E2 Printer 5000 5 15
E3 Camera 7000 20 18
E5 Smartphones 9000 6 10
E6 TV 25000 30 30
a) 7000
b) 30000
c) 25000
d) 9000
Answer: d
Explanation: The query prints the third costliest product in the given table. The use of (where
2=) and comparison of the table with two aliases results in printing 9000
TABLE: products
a) 10
b) 8
c) 9
d) 7
Answer: b
Explanation: Initially the table contains 6 rows. Applying the Join condition results in rows
increasing to 12 in the table. The next condition (where p1.brand =p.brand) is applied and the
table is reduced to 8 rows where the duplicate values of P1002 and P1003 are removed. Finally,
the Count function returns the number of rows present in the table. So, 8 is fetched as the
result.
15) From the following options for the given Query, which should be placed in the Query's
empty portion to choose the "Department_name" that contains Engineering as its ending string?
SELECT Student_name FROM College WHERE Department_name LIKE ' _____ Engineering';
a) ?
b) &
c) %
d) ==
Answer: c
Explanation: The Like operator uses the ‘%’ wildcard in conjunction with it. The ‘%’ represents
zero or multiple characters. Here, the Syntax for printing the required output is LIKE
‘%Engineering’.
Choose the correct function to use in the blank portion to find the mean of the Intern's Stipend
a) AVG(Stipend)
b) SUM(Stipend)
c) COUNT(Stipend)
d) MEAN(Stipend)
Answer: a
Explanation: The AVG function returns the mean value or average value of any numeric column.
17) Which of the given query should be used to find all the batsmen who participated in ODI
World Cup 2015 but not in the 2016 T20 World Cup?
Answer: b
Explanation: In the Inner query, the NOT IN Operator excludes all the batsmen who participated
in the T20 World Cup in 2016. Then the result is fed to the outer query and the required output
where the batsmen who participated only in the 2015 ODI World Cup is displayed.
TABLE: products
Output:
Total_Count
Answer: c
Explanation: Initially the table contains 6 rows for pid. The first condition (brand='Britannia')
results in reducing the table to 3 rows as there are only three pid rows that contain Britannia as
their brand. The next condition is executed and the same three rows which contain the pid such
as P1001, P1001 and P1002 remain. The COUNT(DISTINCT(pid)) prints only distinct values
out of the three remaining rows. So, 2 is fetched as the result.
TABLE: Account_Holder
A2 Elvin Mumbai
A3 Jaydev Nagpur
A4 Axar Delhi
TABLE: Bank_Details
B1 HDFC Chennai
B2 ICICI Kanpur
B3 PNB Mumbai
B4 Axis Kolkata
TABLE: Account_Info
Query:
How many number of rows will be obtained as output after successfully executing the given
query?
a) 1
b) 4
c) 3
d) 2
Answer: b
Explanation:
20) Consider the tables Account_Holder, Bank_Details and Account_Info given below
TABLE: Account_Holder
A1 Aditi Chennai
A2 Elvin Mumbai
A3 Jaydev Nagpur
A4 Axar Delhi
TABLE: Bank_Details
B1 HDFC Chennai
B2 ICICI Kanpur
B3 PNB Mumbai
B4 Axis Kolkata
TABLE: Account_Info
a)
Name Bank_Name
Elvin PNB
Jaydev ICICI
b)
Name Bank_Name
Jaydev ICICI
Axar HDFC
Aditi Axis
Aditi ICICI
c)
Name Bank_Name
Jaydev ICICI
Elvin PNB
d)
Name Bank_Name
Aditi Axis
Aditi ICICI
Axar HDFC
Jaydev ICICI
Answer: c
Explanation: The Inner join performed on the three tables using the common columns returns 6
rows as output. Then the next condition where the account type is not part of either Savings or
Recurring deposits is executed which results in only 2 rows ( Acct_Id - A2, A3). Then the Order
By Keyword sorts the Name in descending order and displays the required output.
TABLE: Account_Holder
A1 Eshwin Chennai
A2 Elvin Mumbai
A3 Elangovan Nagpur
A4 Axar Delhi
TABLE: Account_Info
Query:
a)
Name
Elvin
Eshwin
b)
Name
Elangovan
Elangovan
Elvin
c)
Name
Elvin
d) No output
Answer: c
Explanation: The Inner join is performed on the given two tables using the common column
‘Acct_id’ which returns 6 rows as output. Then the next condition (Like ‘%i%’) reduces the table
to 3 rows as there are only three values that contain the character ‘i’. Then the final condition
( balance > 20000) returns only one row as the final output as the other 2 rows contain a
balance less than or equal to 20000. So, only one row with the name ‘Elvin’ will be fetched as
the output.
TABLE: Items
TABLE: Buyer
B1 Jack Delhi
B2 John Bangalore
B3 Sam Mumbai
B4 Andrew Bangalore
B5 Anne Delhi
B6 Maria Mumbai
B7 Jeny Bangalore
TABLE: Item_order
1001 B2 I4 2 Delhi
1002 B3 I2 5 Bangalore
1003 B5 I1 3 Bangalore
1004 B2 I4 1 Bangalore
1005 B1 I3 9 Mysore
1006 B2 I1 8 Mumbai
1007 B5 I6 4 Chennai
1008 B5 I7 4 Chennai
1009 B5 I8 5 Mumbai
1010 B6 I5 6 Mysore
Query:
a)
1001 B2 I4
1004 B2 I4
b)
1002 B3 I2
1004 B2 I4
c)
1002 B3 I2
d)
No output
Answer: a
Explanation: The tables Items and Item_order are joined using the common column ‘item_id’
and after applying the condition (Category = 'Home Decor') the table fetches 3 rows with
order_id as 1001,1002,1004 as output. The next condition is applied and the final output
contains 2 rows as they are the rows that satisfy the condition (count(io.Buyer_id) > 1).
TABLE: Items
TABLE: Buyer
B1 Jack Delhi
B2 John Bangalore
B3 Sam Mumbai
B4 Andrew Bangalore
B5 Anne Delhi
B6 Maria Mumbai
B7 Jeny Bangalore
TABLE: Item_order
1001 B2 I4 2 Delhi
1002 B3 I2 5 Bangalore
1003 B5 I1 3 Bangalore
1004 B2 I4 1 Bangalore
1005 B1 I3 9 Mysore
1006 B2 I1 8 Mumbai
1007 B5 I6 4 Chennai
1008 B5 I7 4 Chennai
1009 B5 I8 5 Mumbai
1010 B6 I5 6 Mysore
Query:
a)
1007 I6 B5
b)
1003 I1 B5
1009 I8 B5
c)
1005 I3 B1
d)
1001 I4 B2
1002 I2 B3
1004 I4 B2
1006 I1 B2
Answer: b
Explanation: Selecting the three required columns from the table ‘Item_order’ for the first inner
subquery fetches five rows as output as there are two rows with id (B1, B5) which has the
location of Delhi which then joined with Item_order table fetches 5 rows as output. Then the next
subquery is executed where item_id (I1, I2, I4, I6, I8, I9) which doesn’t have the ‘utilities’
category as one of their values are fetched which then further executed with the previous
subquery with AND operator reduces it to three rows. The final subquery is executed next,
where the rows excluding the values which contain the Shipping_Destination as ‘Chennai’ is
fetched. So, the final table is reduced to 2 rows and they are displayed.
TABLE: Items
TABLE: Buyer
B1 Jack Delhi
B2 John Bangalore
B3 Sam Mumbai
B4 Andrew Bangalore
B5 Anne Delhi
B6 Maria Mumbai
B7 Jeny Bangalore
TABLE: Item_order
1001 B2 I4 2 Delhi
1002 B3 I2 5 Bangalore
1003 B5 I1 3 Bangalore
1004 B2 I4 1 Bangalore
1005 B1 I3 9 Mysore
1006 B2 I1 8 Mumbai
1007 B5 I6 4 Chennai
1008 B5 I7 4 Chennai
1009 B5 I8 5 Mumbai
1010 B6 I5 6 Mysore
Query:
a) Dream Catcher
b) Dream Catcher, Cinnamon Candles, Crystal Chariot
c) Cinnamon Candles, Crystal Chariot
d) None of the above
Answer: a
TABLE: Items
TABLE: Buyer
B1 Jack Delhi
B2 John Bangalore
B3 Sam Mumbai
B4 Andrew Bangalore
B5 Anne Delhi
B6 Maria Mumbai
B7 Jeny Bangalore
TABLE: Item_order
1001 B2 I4 2 Delhi
1002 B3 I2 5 Bangalore
1003 B5 I1 3 Bangalore
1004 B2 I4 1 Bangalore
1005 B1 I3 9 Mysore
1006 B2 I1 8 Mumbai
1007 B5 I6 4 Chennai
1008 B5 I7 4 Chennai
1009 B5 I8 5 Mumbai
1010 B6 I5 6 Mysore
Query:
a) 7
b) 6
c) 5
d) 4
Answer: d
TABLE: Items
TABLE: Buyer
B1 Jack Delhi
B2 John Bangalore
B3 Sam Mumbai
B4 Andrew Bangalore
B5 Anne Delhi
B6 Maria Mumbai
B7 Jeny Bangalore
TABLE: Item_order
1001 B2 I4 2 Delhi
1002 B3 I2 5 Bangalore
1003 B5 I1 3 Bangalore
1004 B2 I4 1 Bangalore
1005 B1 I3 9 Mysore
1006 B2 I1 8 Mumbai
1007 B5 I6 4 Chennai
1008 B5 I7 4 Chennai
1009 B5 I8 5 Mumbai
1010 B6 I5 6 Mysore
Query:
a)
Shipping_Destination Quantity
Mysore 9
Mysore 6
Chennai 4
b)
Shipping_Destination Quantity
Mumbai 8
Bangalore 5
Mumbai 5
Chennai 4
Bangalore 3
Delhi 2
Bangalore 1
c)
Shipping_Destination Quantity
Chennai 4
Mysore 6
Mysore 9
d)
Shipping_Destination Quantity
Bangalore 1
Delhi 2
Bangalore 3
Chennai 4
Bangalore 5
Mumbai 5
Mumbai 8
Answer: a
Table: Catalogue
T1 P1 200
T1 P2 100
T1 P3 150
T2 P4 250
T2 P5 300
T3 P1 300
T3 P2 200
T3 P3 350
T3 P4 300
Table: Traders
Table: Parts
P1 Door Wood
P2 Coffin Wood
P3 Door Steel
P4 Chair Steel
P5 Chair Wood
Query:
SELECT T.T_no, T.T_name FROM Traders T, Catalogue C WHERE T.T_no = C.T_no AND
Price > (SELECT AVG (Price) FROM Catalogue WHERE Parts_no = 'P4' GROUP BY
Parts_no);
a) 5
b) 4
c) 0
d) 2
Table: Runs
Query:
SELECT Count(*) FROM ((SELECT Player, Team FROM Runs) AS S NATURAL JOIN
(SELECT Team, Runs_Scored FROM Runs) AS T);
a) 5
b) 3
c) 6
d) None of the above
0 AC 1200
1 AC 1201
2 SC 1201
5 AC 1203
1 SC 1204
3 AC 1202
Table: Traveller
0 Suresh 45
1 Ram 46
2 Axar 47
3 Rahul 59
Query:
SELECT T_id FROM Booking WHERE class ='AC' AND EXISTS (SELECT * FROM Traveller
WHERE Age > 45 AND Traveller.T_id = Booking.T_id);
a) 1,2
b) 1,0
c) 1,5
d) 1,3
Which of the following query would display the last name of all the faculties where the second
letter in the first name is ‘e’?