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

SQL Interview Question JOINs

Uploaded by

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

SQL Interview Question JOINs

Uploaded by

Daniel Wu
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Registrations Logins

id name id name
-- ---- -- ----
1 Bobby 1 Yolanda
2 Xavier 2 Bobby
3 Albert 3 William
4 Zack 4 Albert

-------------------------------------------------------------------------

INNER JOIN

SELECT * FROM Registrations


INNER JOIN Logins
ON Registrations.name = Logins.name

id name id name
-- ---- -- ----
1 Albert 2 Albert
3 Bobby 4 Bobby

-------------------------------------------------------------------------

FULL OUTER JOIN

SELECT * FROM Registrations


FULL OUTER JOIN Logins
ON Registrations.name = Logins.name

id name id name
-- ---- -- ----
1 Albert 2 Albert
2 Xavier null null
3 Bobby 4 Bobby
4 Zack null null
null null 1 Yolanda
null null 3 William

-------------------------------------------------------------------------

SELECT * FROM Registrations


LEFT OUTER JOIN Logins
ON Registrations.name = Logins.name
WHERE Logins.id IS null

id name id name
-- ---- -- ----
2 Xavier - -
4 Zack - -

SELECT * FROM Registrations


FULL OUTER JOIN Logins
ON Registrations.name = Logins.name
WHERE Registrations.id IS null
OR Logins.id IS null

id name id name
-- ---- -- ----
2 Xavier -- ---
4 Zack -- ---
-- --- 1 Yolanda
-- --- 3 William

------------------------------------------------------------------------

Interview Question 1
Let’s continue with our theme of registrations and logins. You’re interviewing at a
large social media company and are given two tables: Registrations and Logins. Each
table consists of a user id and a date timestamp of the event. The first 4 rows of
each table are shown below:
Registrations Logins
id date id date
-- ---- -- ----
1 2015-01-10 1 2015-02-12
3 2015-01-12 1 2015-03-04
5 2015-02-02 3 2015-03-15
9 2015-02-09 4 2015-04-22

##
Write a query that will give the number of times each user logged in within their
first week of registration (i.e. within 7 days after registering for the site).

Interview Question 2
You are interviewing for a position at a CRM company. Your given two tables,
Employees and Departments. The Employees table consists of the employee id number,
the department id, the supervisor id, the employee name, and their salary. The
Departments table consists of the department id and the department name. Below are
the an example two rows of each table.
Employees Departments
empid deptid supid name salary deptid name
-- -- -- --- ---- --- ----
1 7 12 Bob 90 7 Marketing
2 8 11 Amy 110 1 Sales

##
Can you list employees by name who have a larger salary than their supervisor?

Interview Question 3
You are interviewing for an online marketplace company and are presented with three
tables:
Table 1: Salespeople
ID Name Age Salary
-- -- -- ---
1 Albert 40 90000
2 Bob 38 88000
Table 2: Customers
ID Name City Industry
-- ----- ----- ---
5 Hooli Sunnyvale V
7 PiedPiper Palo Alto C
Table 3: Orders
Number Order_Date Cust_id Salesperson_id Amount
-- ----- ----- ------ ------
10 3/2/12 5 1 11000
20 4/8/14 7 2 50000

##
Write a query to list the names of all the salespeople that have an order with
Hooli.

##
Write a query the returns the names of all salespeople that do not have an order
with Hooli.

Here are my answers:

Interview Question 1:
(Note: in my tables both log_date and reg_date are in “date” format, so the
difference between them is counted in days)

SELECT Logins.id, COUNT(Logins.id)


FROM Logins
JOIN Registrations ON Logins.id = Registrations.id
WHERE (log_date — reg_date) <= 7
GROUP BY Logins.id

Interview Question 2:
We are not given supervisors’ salary, some data might be missing, can’t answer the
question without it (or I don’t understand something).

Interview Question 3:
Names of salespeople that have an order with Hooli:

SELECT Salespeople.Name
FROM Salespeople
WHERE Salespeople.ID IN
(SELECT Salesperson_id
FROM Orders
JOIN Customers ON Customers.ID = Orders.Cust_id
WHERE Customers.Name = ‘Hooli’);
Names of salespeople that don’t have an order with Hooli:
SELECT Salespeople.Name
FROM Salespeople
WHERE Salespeople.ID NOT IN
(SELECT Salesperson_id
FROM Orders
JOIN Customers ON Customers.ID = Orders.Cust_id
WHERE Customers.Name = ‘Hooli’);

For the 3rd question, it’s also possible to use JOIN twice:
SELECT Salespeople.Name
FROM Orders
JOIN Customers ON Customers.ID = Orders.Cust_id
JOIN Salespeople ON Salespeople.ID = Orders.Salesperson_id
WHERE Customers.Name = ‘Hooli’;

You might also like