ABES Engineering College, Ghaziabad (032)
InfyTQ Training Topic Name: SQL Joins, Subqueries, GroupBy
Q1 Table Salesman
SID SNAME LOC
1 Aman London
2 Shreya Paris
3 John Mumbai
4 Jack Chicago
5 ayaz London
Table Sale
ID SaleID AMOUNT
1 1001 2000
5 1002 9000
4 1003 8000
1 1004 7500
2 1005 6200
SELECT SM.SID, SNAME FROM SALESMAN SM INNER JOIN SALE S ON
S.ID = SM.SID WHERE LOCATION IN( Select location from Salesman where
SNAME like ‘%_ a%’;
How many rows will be retrieved from the above query?
A 3
B 2
C 5
D 4
AN
DL M
Explanation
Q2 Consider the table account and branch given below:
Table: account
Cid Account Ifsc Location Amount A-type
C901 10530002231 B1053 Bhopal 140000 Saving
C902 12340004235 B1234 Noida 225000 Current
C903 87900002367 B5663 Ghaziabad 79000 Saving
C904 56630003454 B5663 Ghaziabad 152000 Saving
C905 78390002343 B7839 Noida 73000 Current
Table: branch
Ifsc B_Name
B5663 PNB
B7839 SBI
B1234 BOI
How many rows will be fetched from the following query?
select b.ifsc from account a inner join branch b on a.ifsc=b.ifsc;
A 2
B 3
C 4
D None of these
AN
DL M
Explanation
Q3 Consider the table account and branch given below:
Table: account
Cid Account Ifsc Location Amount A-type
C901 10530002231 B1053 Bhopal 140000 Saving
C902 12340004235 B1234 Noida 225000 Current
C903 87900002367 B5663 Ghaziabad 79000 Saving
C904 56630003454 B5663 Ghaziabad 152000 Saving
C905 78390002343 B7839 Noida 73000 Current
Table: branch
Ifsc B_Name
B5663 PNB
B7839 SBI
B1234 BOI
How many rows will be returned when the following query is executed?
Select cid, a.ifsc from account a right outer join branch b on a.ifsc = b.ifsc where atype
= ‘Current’;
A 0
B 1
C 2
D Error
AN
DL M
Explanation
Q4 Table: Employee
CID FirstName LastName Salary Address
1 Ana alphy 60000 Noida
2 anil Saxena 67000 Pune
2 Swati Goel 48000 Ghaziabad
4 Prerna bhardwaj 56000 Noida
5 Ashu Sharma 76000 Pune
4 Priyanka Bhardwaj 55000 Leh
The right query to display the name of the employee who is having a salary greater
than that of Swati is?
A Select firstname from employee where salary > ‘Swati’;
B Select firstname from employee where salary != (select salary from employee where
first name NOT LIKE ‘S%’);
C Select firstname from employee where salary > (select salary from employee where
first name = ‘Swati”);
D Both B and C
AN
DL M
Explanation
Q5 Table B
ID NAME CITY
15 Shreya Delhi
25 Harish Noida
98 Rohit Noida
99 Rohit Agra
Consider the above tables B. How many tuples does the result of the following SQL
query contain?
Delete FROM B WHERE id >ANY(SELECT id FROM B WHERE CITY like ‘%i
%’);
A 2
B 3
C 1
D No output
AN
DL M
Explanation
Q6 Table A
ID NAME AGE
12 Arun 60
31 Shreya 24
99 Rohit 11
Table B
ID NAME CITY
15 Shreya Delhi
25 Harish Delhi
98 Rohit Noida
99 Rohit Agra
Consider the above tables A and B. How many tuples does the result of the
following SQL query contain?
SELECT A.id FROM A WHERE A.id > ALL (SELECT B.id FROM B WHERE
B.CITY like ‘%_l_%’);
A 2
B 1
C No output
D 3
AN
DL M
Explanation
Q7 Display salary for each department and job of employees along with department code
and Job profile.
Expected Output:
Department Code JOB Total Salary
20 MANAGER 2975
10 PRESIDENT 5000
20 ANALYST 6000
10 CLERK 1300
10 MANAGER 2450
30 SALESMAN 5600
20 CLERK 1900
30 MANAGER 2850
30 CLERK 950
Which one of these is correct for the given output?
A SELECT DEPTNO "Department Code", SUM(SAL) "Total
Salary" FROM emp GROUP BY DEPTNO, JOB;
B SELECT DEPTNO Department Code, JOB, SUM(SAL) "Total
Salary" FROM emp GROUP BY DEPTNO, JOB;
C SELECT DEPTNO "Department Code", JOB, SUM(SAL) "Total
Salary" FROM emp GROUP BY DEPTNO, SUM(SAL) "Total
Salary";
D SELECT DEPTNO "Department Code", JOB, SUM(SAL) "Total
Salary" FROM emp GROUP BY JOB;
AN
DL M
Explanation
Q8 Student
ID SNAME DEPT LID
1 James Potter FSI L1101
2 Ethan McCarty ETA L1102
3 Emily Rayner ETA L1103
How many rows will be fetched by the following query?
SELECT COUNT(ID), Lid FROM Student GROUP BY Lid HAVING COUNT(ID) >= 1
ORDER BY COUNT(ID) DESC;
A 1
B 2
C 3
D 4
AN
DL M
Table 1:
RollNo StudentName StudentAge
112 Arjun 19
157 Shelya 22
259 Ronit 21
Table 2:
RollNo StudentName StudentAge
157 Shelya 22
251 Hashit 40
198 Ronit 20
259 Ronit 21
Table 3:
RollNo StudentPhone StudentGroup
100 12200 02
259 12100 01
9 How many tuples does return by the following SQL query?
SELECT 1.RollNo FROM 1 WHERE 1.StudentAge > ALL (SELECT 2.StudentAge FROM 2
WHERE 2.StudentName = 'arjun');
A 4
B 3
C 2
D 1
AN
DL M
10 What will be the output of the following query?
SELECT 1.RollNo, 2.StudentName, 1.StudentAge FROM 1 INNER JOIN 2 ON 1.RollNo =
2.RollNo;
RollNo StudentName StudentAge
259 Ronit 21
RollNo StudentName StudentAge
157 Shelya 22
RollNo StudentName StudentAge
157 Shelya 22
259 Ronit 21
RollNo StudentName StudentAge
112 Arjun 19
157 Shelya 22
259 Ronit 21
AN
DL M
11 How many tuples does return by the following SQL query?
SELECT 1.RollNo, 2.StudentName, 1.StudentAge FROM 1 LEFT JOIN 2 ON 1.RollNo =
2.RollNo;
A 4
B 3
C 2
D 1
AN
DL M
12 Write SQL query to get the names of students who have scored second highest marks.
A SELECT stu_name FROM Student WhERE Score = (SELECT Distinct Top(1) Score FROM
Student WHERE Score Not In
(SELECT Distinct Top(1) Score FROM Student ORDER BY Score Descending) ORDER BY
Score descending);
B SELECT stu_name FROM Student WhERE Score = (SELECT Distinct Top(1) Score FROM
Student WHERE Score Not In
(SELECT Distinct Top(1) Score FROM Student ORDER BY Score Desc);
C SELECT stu_name FROM Student WhERE Score = (SELECT Distinct Top(1) Score FROM
Student WHERE Score Not In
(SELECT Distinct Top(1) Score FROM Student ORDER BY Score Desc) ORDER BY Score
desc);
D SELECT stu_name FROM Student WhERE Score = (SELECT Distinct Top(1) Score FROM
Student WHERE Score Not In
(SELECT Distinct Top(1) Score FROM Student);
AN
DL M
13 Member Table:
Memberid Name StartDate EndDate Status
1 Marc 1-JAN-15 Active
2 Marvin 1-FEB-15 ACTIVE
3 Bailey 1-JAN-15 Active
4 Maria 1-MAR-15 15-APR-15 Cancelled
5 Ashley 1-JAN-15 Active
6 Edgar 1-MAR-15 15-MAR-15 Cancelled
7 DARCY 1-APR-15 Active
How many rows will be returned by the query below?
SELECT Name FROM Member WHERE LENGTH(Name) = 5 AND NVL(EndDate, '20-Apr-
2015') - StartDate < 30;
A 0
B 1
C 2
D 3
AN
DL M
SQL Queries
1. Write a SQL query to find all duplicate emails in a table named Person.
+----+---------+
| Id | Email |
+----+---------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
+----+---------+
For example, your query should return the following for the above table:
+---------+
| Email |
+---------+
| [email protected] |
+---------+
Note: All emails are in lowercase.
2. Table: Person
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| PersonId | int |
| FirstName | Varchar |
| LastName | Varchar |
+-------------+---------+-------------------+
PersonId is the primary key column for this table.
Table: Address
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | Varchar |
| State | Varchar |
+-------------+---------+
AddressId is the primary key column for this table.
Write a SQL query for a report that provides the following information for each person in the Person
table, regardless if there is an address for each of those people:
FirstName, LastName, City, State
3. Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL
query to find all customers who never order anything.
Table: Customers.
+----+-------+
| Id | Name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+
Table: Orders.
+----+------------+
| Id | CustomerId |
+----+------------+
|1 |3 |
|2 |1 |
+----+------------+
Using the above tables as example, return the following:
+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+
4. Write a SQL query to delete all duplicate email entries in a table named Person, keeping only
unique emails based on its smallest Id.
+----+------------------+
| Id | Email |
+----+------------------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
+----+------------------+
Id is the primary key column for this table.
For example, after running your query, the above Person table should have the following rows:
+----+------------------+
| Id | Email |
+----+------------------+
| 1 | [email protected] |
| 2 | [email protected] |
+----+------------------+
Note:
Your output is the whole Person table after executing your sql. Use delete statement.
5. Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared
to its previous (yesterday's) dates.
For example, return the following Ids for the above Weather table:
+----+
| Id |
+----+
|2 |
|4 |
+----+