Week 2 - SQL Joins, Subqueries, Groupby
Week 2 - 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
A 3
B 2
C 5
D 4
AN
DL M
Explanation
Q2 Consider the table account and branch given below:
Table: account
Table: branch
Ifsc B_Name
B5663 PNB
B7839 SBI
B1234 BOI
A 2
B 3
C 4
D None of these
AN
DL M
Explanation
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
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:
Q8 Student
A 1
B 2
C 3
D 4
AN
DL M
Table 1:
112 Arjun 19
157 Shelya 22
259 Ronit 21
Table 2:
157 Shelya 22
251 Hashit 40
198 Ronit 20
259 Ronit 21
Table 3:
100 12200 02
259 12100 01
A 4
B 3
C 2
D 1
AN
DL M
259 Ronit 21
157 Shelya 22
259 Ronit 21
112 Arjun 19
157 Shelya 22
259 Ronit 21
AN
DL M
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:
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 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+
For example, your query should return the following for the above table:
+---------+
| Email |
+---------+
| a@b.com |
+---------+
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 |
+----+------------+
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 | john@example.com |
| 2 | bob@example.com |
| 3 | john@example.com |
+----+------------------+
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 | john@example.com |
| 2 | bob@example.com |
+----+------------------+
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 |
+----+