SQL Qus
SQL Qus
net
45
MOST ASKED SQL QUESTIONS
in
Curated by
www.tutort.net
Question. 1
addressId int
personId int
personId int
lastName varchar
city varchar
firstName varchar
state varchar
personId is the primary key column for this addressId is the primary key column for this
table. This table contains information about table. Each row of this table contains
the ID of some persons and their first and information about the city and state of one
last names. person with ID = PersonId.
From To
From Tier III college to
at Walmart
www.tutort.net
Question. 2
Table Employee
id is the primary key column for this table.
Column Name Type
Each row of this table contains information
about the salary of an employee.
id int
salary int
Question. 3
Table Employee
id is the primary key column for this table.
Column Name Type
Each row of this table contains information
about the salary of an employee.
id int
salary int
Question. 4
Rank Scores
Write an SQL query to rank the scores. The ranking should be calculated according to the
following rules
The scores should be ranked from the highest to the lowest
If there is a tie between two scores, both should have the same ranking
After a tie, the next ranking number should be the next consecutive integer value. In other
words, there should be no holes between ranks.
Table Scores
id int
score decimal
Question. 5
Consecutive Numbers
Write an SQL query to find all numbers that appear at least three times consecutively.
Return the result table in any order.
Table Logs
id is the primary key for this table.
Column Name Type id is an autoincrement column.
id int
num varchar
Question. 6
Table Employee
id is the primary key column for this table.
Column Name Type Each row of this table indicates the ID of an
employee, their name, salary, and the ID of
id int
their manager.
name varchar
salary int
managerId int
Question. 7
Duplicate Emails
Write an SQL query to report all the duplicate emails. Note that it's guaranteed that the email
field is not NULL.
Return the result table in any order.
Table person
Column Name Type id is the primary key column for this table.
Each row of this table contains an email. The
id int emails will not contain uppercase letters.
email varchar
Question. 8
Customers Who Never Order
Write an SQL query to report all customers who never order anything.
Return the result table in any order.
Question. 9
id int
id int
name varchar
name varchar
From To
Pritom Mazumdar
Software Engineer at Microsoft
www.tutort.net
Question. 10
A company's executives are interested in seeing who earns the most money in each of the
company's departments. A high earner in a department is an employee who has a salary in the
Write an SQL query to find the employees who are high earners in each of the departments.
id int
id int
name varchar
name varchar
Question asked in
Practice
From To
Placed with
Question. 11
Delete Duplicate Emails
Write an SQL query to delete all the duplicate emails, keeping only one unique email with the
smallest id. Note that you are supposed to write a DELETE statement and not a SELECT one.
After running your script, the answer shown is the Person table. The driver will first compile and
run your piece of code and then show the Person table. The final order of the Person table does
not matter.
Table Person
Column Name Type
id int
email varchar
id is the primary key column for this table.
Each row of this table contains an email. The emails will not contain
uppercase letters.
JPMorgan Chase
Company
www.tutort.net
Question. 12
Rising Temperature
Write an SQL query to find all dates' Id with higher temperatures compared to its previous dates
(yesterday).
id int
recordDate date
temperature int
Question. 13
Write a SQL query to find the cancellation rate of requests with unbanned users (both client and
driver must not be banned) each day between "2013-10-01" and "2013-10-03". Round
Cancellation Rate to two decimal points.
Table Users
Column Name Type
users_id int
banned enum
role enum
From To
Placed from
Aditya Tier III college
www.tutort.net
Question. 14
customer with id = 2.
Table Customer
referee_id int
Question asked in
Practice
Question. 15
number of orders.
The test cases are generated so that exactly one customer will have placed more orders than any
other customer.
Table Orders
name of a customer.
order_number int
customer_number int
Question asked in
Practice
www.tutort.net
Question. 16
Big Countries
A country is big if
it has an area of at least three million (i.e., 3000000 km2), o
it has a population of at least twenty-five million (i.e., 25000000).
Write an SQL query to report the name, population, and area of the big countries.
Table World
name varchar
continent varchar
area int
population int
gdp bigint
From To
Question. 17
Classes More Than 5 Students
Write an SQL query to report all the classes that have at least five students.
Table Courses
(student, class) is the primary key column for
Column Name Type this table.
Each row of this table indicates the name of a
student varchar
student and the class in which they are enrolled.
class varchar
Question. 18
Table Stadium
visit_date is the primary key for this table.
Column Name Type Each row of this table contains the visit date and visit id to
the stadium with the number of people during the visit.
id int
No two rows will have the same visit_date, and as the id
visit_date date increases, the dates increase as well.
people int
Question. 19
Sales Person
Write an SQL query to report the names of all the salespersons who did not have any orders
related to the company with the name "RED".
Return the result table in any order.
ales_id is the primary key column for this table.
order_id is the primary key column for this table.
Each row of this table indicates the name and the com_id is a foreign key to com_id from the Company
ID of a salesperson alongside their salary, table. sales_id is a foreign key to sales_id from the
commission rate, and hire date. SalesPerson table. Each row of this table contains
information about one order. This includes the ID of the
company, the ID of the salesperson, the date of the
order, and the amount paid.
Table Company
com_id is the primary key column for this table.
Column Name Type Each row of this table indicates the name and the
ID of a company and the city in which the
com_id int company is located.
name varchar
city varchar
Question. 20
Tree Node
Each node in the tree can be one of three types
"Leaf": if the node is a leaf node
"Root": if the node is the root of the tree
"Inner": If the node is neither a leaf node nor a root node.
Write an SQL query to report the type of each node in the tree.
Return the result table in any order.
Table Tree
id int
p_id int
Question. 21
Table Cinema
id int
movie varchar
description varchar
rating float
Question. 22
Exchange Seats
Write an SQL query to swap the seat id of every two consecutive students. If the number of
students is odd, the id of the last student is not swapped.
Table Seat
id is the primary key column for this table.
Column Name Type
Each row of this table indicates the name and
the ID of a student.
id int
id is a continuous increment.
student varchar
Question. 23
Swap Salary
Write an SQL query to swap all 'f' and 'm' values (i.e., change all 'f' values to 'm' and vice versa) with a
single update statement and no intermediate temporary tables.
N ote that you ust rite a single u date state ent do not rite any select state ent or this roble .
m w p m , w m f p m
Table Salary
id is the primary key for this table.
Column Name Type
The sex column is ENUM value of type ('m', 'f').
The table contains information about an employee.
id int
name varchar
sex ENUM
salary int
Question. 24
Actors and Directors Who
Cooperated At Least Three Times
Write a SQL query for a report that provides the pairs (actor_id, director_id) where
the actor has cooperated with the director at least three times.
at Mastercard
Company
www.tutort.net
Question. 25
Question. 26
Write an SQL query to report the first login date for each player.
Table Activity
player_id int
device_id int
event_date date
games_played int
Question asked in
Practice
From To
Placed with
Question. 27
Write an SQL query to find the daily active user count for a period of 30 days ending
2019-07-27 inclusively. A user was active on someday if they made at least one
Table Activity
user_id int
session_id int
activity_date date
activity_type enum
There is no primary key for this table, it may have duplicate rows.
The table shows the user activities for a social media website.
Question asked in
Practice
From To
Placed with
Question. 28
Article Views I
Write an SQL query to find all the authors that viewed at least one of their own articles.
Table Activity
article_id int
author_id int
viewer_id int
view_date date
There is no primary key for this table, it may have duplicate rows.
Each row of this table indicates that some viewer viewed an article
Note that equal author_id and viewer_id indicate the same person.
Question asked in
Practice
From To
Placed with
Question. 29
Market Analysis I
Write an SQL query to find for each user, the join date and the number of orders they
order_id int
user_id int
order_date date
join_date date
item_id int
favorite_brand varchar
buyer_id int
seller_id int
user_id is the primary key of this table.
Users table.
Table Items
item_id int
item_brand varchar
Question asked in
Practice
www.tutort.net
Question. 30
Write an SQL query to reformat the table such that there is a department id column
Table Department
id int
revenue int
month varchar
(id, month) is the primary key of this table. The table has
["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"].
Question asked in
Practice
From To
Question. 31
Capital Gain/Loss
Write an SQL query to report the Capital gain/loss for each stock.
The Capital gain/loss of a stock is the total gain or loss after buying and selling the
Table Stocks
stock_name varchar
operation enum
operation_day int
price int
Each row of this table indicates that the stock which has stock_name
that each 'Buy' operation for a stock has a corresponding 'Sell' operation
in an upcoming day.
Question asked in
Practice
Service Based
Arpita Basak
Analyst at Goldman Sachs
Company
www.tutort.net
Question. 32
Top Travellers
two or more users traveled the same distance, order them by their name in
ascending order.
id int id int
distance int
id is the primary key for this table. name is the name of the user.
distance "distance".
Question asked in
Practice
From To
Placed with
Microsoft
www.tutort.net
Question. 33
The sold products names for each date should be sorted lexicographically.
Return the result table ordered by sell_date.
Table Activities
sell_date date
product varchar
Question. 34
Table Patients
patient_id int
patient_name varchar
conditions varchar
Question. 35
Service Based
Arpita Basak
Analyst at Goldman Sachs
Company
www.tutort.net
Question. 36
than 10000. The balance of an account is equal to the sum of the amounts of all
amount int
transacted_on date
account is the primary key for this table.
money.
Question asked in
Practice
From To
Placed with
Question. 37
are lowercase.
Table Users
name varchar
Question asked in
Practice
Question. 38
Write an SQL query that will, for each date_id and make_name, return the number of distinct
Table DailySales
sold and the IDs of the lead and partner it was sold to.
date_id date
The name consists of only lowercase English letters.
make_name varchar
lead_id int
partner_id int
Question asked in
Practice
www.tutort.net
Question. 39
Table Followers
user_id int
follower_id int
Question. 40
Table Employees
emp_id int
event_day date
in_time int
out_time int
Question. 41
Recyclable and Low Fat Products
Write an SQL query to find the ids of products that are both low fat and recyclable.
Table Users
product_id is the primary key for this table.
Column Name Type
low_fats is an ENUM of type ('Y', 'N') where 'Y' means this
product is low fat and 'N' means it is not.
product_id int
recyclable is an ENUM of types ('Y', 'N') where 'Y' means this
low_fats enum product is recyclable and 'N' means it is not.
recyclable enum
Question. 42
Table Products
product_id is the primary key for this table.
Column Name Type
Each row in this table indicates the product's price in 3
different stores: store1, store2, and store3.
product_id int
If the product is not available in a store, the price will be null
store1 int in that store's column.
store2 int
store3 int
Question. 43
Write an SQL query to calculate the bonus of each employee. The bonus of an
employee is 100% of their salary if the ID of the employee is an odd number and the
employee name does not start with the character 'M'. The bonus of an employee is 0
otherwise.
Table Employees
employee_id int
name varchar
salary int
event_day is the day at which this event happened, in_time is the minute at which the employee entered the
office, and out_time is the minute at which they left the office.
It is guaranteed that no two events on the same day intersect in time, and in_time < out_time.
Question asked in
Practice
From To
Placed with
Microsoft
www.tutort.net
Question. 44
Table Logins
user_id int
time_stamp datetime
Question. 45
employee_id is the primary key for this table.
employee_id is the primary key for this table.
Each row of this table indicates the name of the Each row of this table indicates the salary of the
employee whose ID is employee_id. employee whose ID is employee_id.
From To
www.tutort.net
Follow us on