175. Combine Two Tables
My Solution:
# Write your MySQL query statement below
select FirstName, LastName, City, State
from Person left join Address
on Person.PersonId = Address.PersonId;
176. Second Highest Salary
My Solution:
# Write your MySQL query statement below
select max(salary) as SecondHighestSalary
from Employee
where salary < (select max(salary) from Employee)
177. Nth Highest Salary
My Solution:
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
set N = N-1;
RETURN (
# Write your MySQL query statement below.
select distinct salary from Employee order by salary desc limit N,1
);
END
178. Rank Scores
My Solution:
# Write your MySQL query statement below
select Scores.Score, s3.Rank
from Scores left join (select s1.Score, count(s1.Score) as Rank
from (select distinct Score from Scores) as s1, (select distinct Score from Scores) as s2
where s1.Score <= s2.Score
group by s1.Score) as s3
on Scores.Score = s3.Score
order by s3.Rank;
180. Consecutive Numbers
My Solution:
# Write your MySQL query statement below
select distinct Num as ConsecutiveNums from (
select
Num,
case
when @prevNum = Num then @count := @count + 1
when (@prevNum := Num) is not null then @count := 1
end n
from Logs, (select @prevNum := NULL) r
order by Id
) a where n >= 3
181. Employees Earning More Than Their Managers
My Solution:
# Write your MySQL query statement below
select e1.Name as Employee
from Employee as e1, Employee as e2
where e1.ManagerId = e2.Id and e1.Salary > e2.Salary;
182. Duplicate Emails
My Solution:
# Write your MySQL query statement below
select Email
from Person
group by Email
having count(*) > 1;
183. Customers Who Never Order
My Solution:
# Write your MySQL query statement below
select Customers.Name as Customers
from Customers
where not exists (select * from Orders where Customers.Id = Orders.CustomerId)
184. Department Highest Salary
My Solution:
# Write your MySQL query statement below
select d.Name as Department, e.Name as Employee, e.Salary
from Employee as e, Department as d
where e.DepartmentId = d.Id
and (e.DepartmentId, e.Salary) in
(select DepartmentId, max(Salary) as max_salary from Employee group by DepartmentID)
185. Department Top Three Salaries
My Solution:
# Write your MySQL query statement below
select d.Name as Department, e1.Name as Employee, e1.Salary
from Employee as e1 join Department as d
on e1.DepartmentId = d.Id
where 3 > (select count(distinct(e2.Salary))
from Employee as e2
where e2.Salary > e1.Salary
and e2.DepartmentId = e1.DepartmentId
)
196. Delete Duplicate Emails
My Solution:
# Write your MySQL query statement below
delete ob1
from Person ob1, Person ob2
where ob1.Email=ob2.Email and ob1.Id>ob2.Id
197. Rising Temperature
My Solution:
# Write your MySQL query statement below
select d1.Id
from Weather as d1, Weather as d2
where To_Days(d1.Date) = To_Days(d2.Date) + 1 and
d1.Temperature > d2.Temperature;
262. Trips and Users
My Solution:
# Write your MySQL query statement below
select t.Request_at as Day, round(sum(case when t.Status like 'cancelled_%' then 1 else 0 end)/count(*),2) as 'Cancellation Rate'
from Trips as t inner join Users as u
on t.Client_Id = u.Users_Id and u.Banned = 'No'
where t.Request_at between '2013-10-01' and '2013-10-03'
group by t.Request_at
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
My Solution:
# Write your MySQL query statement below
select FirstName, LastName, City, State
from Person left join Address
on Person.PersonId = Address.PersonId;
176. Second Highest Salary
Write a SQL query to get the second highest salary from the
Employee
table.+----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+
For example, given the above Employee table, the second highest salary is
200
. If there is no second highest salary, then the query should return null
.My Solution:
# Write your MySQL query statement below
select max(salary) as SecondHighestSalary
from Employee
where salary < (select max(salary) from Employee)
177. Nth Highest Salary
Write a SQL query to get the nth highest salary from the
Employee
table.+----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+
For example, given the above Employee table, the nth highest salary where n = 2 is
200
. If there is no nth highest salary, then the query should return null
.My Solution:
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
set N = N-1;
RETURN (
# Write your MySQL query statement below.
select distinct salary from Employee order by salary desc limit N,1
);
END
178. Rank Scores
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no "holes" between ranks.
+----+-------+ | Id | Score | +----+-------+ | 1 | 3.50 | | 2 | 3.65 | | 3 | 4.00 | | 4 | 3.85 | | 5 | 4.00 | | 6 | 3.65 | +----+-------+
For example, given the above
Scores
table, your query should generate the following report (order by highest score):+-------+------+ | Score | Rank | +-------+------+ | 4.00 | 1 | | 4.00 | 1 | | 3.85 | 2 | | 3.65 | 3 | | 3.65 | 3 | | 3.50 | 4 | +-------+------+
My Solution:
# Write your MySQL query statement below
select Scores.Score, s3.Rank
from Scores left join (select s1.Score, count(s1.Score) as Rank
from (select distinct Score from Scores) as s1, (select distinct Score from Scores) as s2
where s1.Score <= s2.Score
group by s1.Score) as s3
on Scores.Score = s3.Score
order by s3.Rank;
180. Consecutive Numbers
Write a SQL query to find all numbers that appear at least three times consecutively.
+----+-----+ | Id | Num | +----+-----+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 1 | | 6 | 2 | | 7 | 2 | +----+-----+
For example, given the above
Logs
table, 1
is the only number that appears consecutively for at least three times.My Solution:
# Write your MySQL query statement below
select distinct Num as ConsecutiveNums from (
select
Num,
case
when @prevNum = Num then @count := @count + 1
when (@prevNum := Num) is not null then @count := 1
end n
from Logs, (select @prevNum := NULL) r
order by Id
) a where n >= 3
181. Employees Earning More Than Their Managers
The
Employee
table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.+----+-------+--------+-----------+ | Id | Name | Salary | ManagerId | +----+-------+--------+-----------+ | 1 | Joe | 70000 | 3 | | 2 | Henry | 80000 | 4 | | 3 | Sam | 60000 | NULL | | 4 | Max | 90000 | NULL | +----+-------+--------+-----------+
Given the
Employee
table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.+----------+ | Employee | +----------+ | Joe | +----------+
My Solution:
# Write your MySQL query statement below
select e1.Name as Employee
from Employee as e1, Employee as e2
where e1.ManagerId = e2.Id and e1.Salary > e2.Salary;
182. Duplicate Emails
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 | +---------+
# Write your MySQL query statement below
select Email
from Person
group by Email
having count(*) > 1;
183. Customers Who Never Order
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 | +-----------+
My Solution:
# Write your MySQL query statement below
select Customers.Name as Customers
from Customers
where not exists (select * from Orders where Customers.Id = Orders.CustomerId)
184. Department Highest Salary
The
Employee
table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id.+----+-------+--------+--------------+ | Id | Name | Salary | DepartmentId | +----+-------+--------+--------------+ | 1 | Joe | 70000 | 1 | | 2 | Henry | 80000 | 2 | | 3 | Sam | 60000 | 2 | | 4 | Max | 90000 | 1 | +----+-------+--------+--------------+
The
Department
table holds all departments of the company.+----+----------+ | Id | Name | +----+----------+ | 1 | IT | | 2 | Sales | +----+----------+
Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, Max has the highest salary in the IT department and Henry has the highest salary in the Sales department.
+------------+----------+--------+ | Department | Employee | Salary | +------------+----------+--------+ | IT | Max | 90000 | | Sales | Henry | 80000 | +------------+----------+--------+
My Solution:
# Write your MySQL query statement below
select d.Name as Department, e.Name as Employee, e.Salary
from Employee as e, Department as d
where e.DepartmentId = d.Id
and (e.DepartmentId, e.Salary) in
(select DepartmentId, max(Salary) as max_salary from Employee group by DepartmentID)
185. Department Top Three Salaries
The
Employee
table holds all employees. Every employee has an Id, and there is also a column for the department Id.+----+-------+--------+--------------+ | Id | Name | Salary | DepartmentId | +----+-------+--------+--------------+ | 1 | Joe | 70000 | 1 | | 2 | Henry | 80000 | 2 | | 3 | Sam | 60000 | 2 | | 4 | Max | 90000 | 1 | | 5 | Janet | 69000 | 1 | | 6 | Randy | 85000 | 1 | +----+-------+--------+--------------+
The
Department
table holds all departments of the company.+----+----------+ | Id | Name | +----+----------+ | 1 | IT | | 2 | Sales | +----+----------+
Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows.
+------------+----------+--------+ | Department | Employee | Salary | +------------+----------+--------+ | IT | Max | 90000 | | IT | Randy | 85000 | | IT | Joe | 70000 | | Sales | Henry | 80000 | | Sales | Sam | 60000 | +------------+----------+--------+
My Solution:
# Write your MySQL query statement below
select d.Name as Department, e1.Name as Employee, e1.Salary
from Employee as e1 join Department as d
on e1.DepartmentId = d.Id
where 3 > (select count(distinct(e2.Salary))
from Employee as e2
where e2.Salary > e1.Salary
and e2.DepartmentId = e1.DepartmentId
)
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 | +----+------------------+
My Solution:
# Write your MySQL query statement below
delete ob1
from Person ob1, Person ob2
where ob1.Email=ob2.Email and ob1.Id>ob2.Id
197. Rising Temperature
Given a
Weather
table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates.+---------+------------+------------------+ | Id(INT) | Date(DATE) | Temperature(INT) | +---------+------------+------------------+ | 1 | 2015-01-01 | 10 | | 2 | 2015-01-02 | 25 | | 3 | 2015-01-03 | 20 | | 4 | 2015-01-04 | 30 | +---------+------------+------------------+For example, return the following Ids for the above Weather table:
+----+ | Id | +----+ | 2 | | 4 | +----+
My Solution:
# Write your MySQL query statement below
select d1.Id
from Weather as d1, Weather as d2
where To_Days(d1.Date) = To_Days(d2.Date) + 1 and
d1.Temperature > d2.Temperature;
262. Trips and Users
The
Trips
table holds all taxi trips. Each trip has a unique Id, while Client_Id and Driver_Id are both foreign keys to the Users_Id at the Users
table. Status is an ENUM type of (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’).+----+-----------+-----------+---------+--------------------+----------+ | Id | Client_Id | Driver_Id | City_Id | Status |Request_at| +----+-----------+-----------+---------+--------------------+----------+ | 1 | 1 | 10 | 1 | completed |2013-10-01| | 2 | 2 | 11 | 1 | cancelled_by_driver|2013-10-01| | 3 | 3 | 12 | 6 | completed |2013-10-01| | 4 | 4 | 13 | 6 | cancelled_by_client|2013-10-01| | 5 | 1 | 10 | 1 | completed |2013-10-02| | 6 | 2 | 11 | 6 | completed |2013-10-02| | 7 | 3 | 12 | 6 | completed |2013-10-02| | 8 | 2 | 12 | 12 | completed |2013-10-03| | 9 | 3 | 10 | 12 | completed |2013-10-03| | 10 | 4 | 13 | 12 | cancelled_by_driver|2013-10-03| +----+-----------+-----------+---------+--------------------+----------+
The
Users
table holds all users. Each user has an unique Users_Id, and Role is an ENUM type of (‘client’, ‘driver’, ‘partner’).+----------+--------+--------+ | Users_Id | Banned | Role | +----------+--------+--------+ | 1 | No | client | | 2 | Yes | client | | 3 | No | client | | 4 | No | client | | 10 | No | driver | | 11 | No | driver | | 12 | No | driver | | 13 | No | driver | +----------+--------+--------+
Write a SQL query to find the cancellation rate of requests made by unbanned clients between Oct 1, 2013 and Oct 3, 2013. For the above tables, your SQL query should return the following rows with the cancellation rate being rounded to two decimal places.
+------------+-------------------+ | Day | Cancellation Rate | +------------+-------------------+ | 2013-10-01 | 0.33 | | 2013-10-02 | 0.00 | | 2013-10-03 | 0.50 | +------------+-------------------+
# Write your MySQL query statement below
select t.Request_at as Day, round(sum(case when t.Status like 'cancelled_%' then 1 else 0 end)/count(*),2) as 'Cancellation Rate'
from Trips as t inner join Users as u
on t.Client_Id = u.Users_Id and u.Banned = 'No'
where t.Request_at between '2013-10-01' and '2013-10-03'
group by t.Request_at
Comments
Post a Comment