SQL Test: Considering The Following Table Structures in Database (Ignore The Absence of PRIMARY Key Constraints)
SQL Test: Considering The Following Table Structures in Database (Ignore The Absence of PRIMARY Key Constraints)
Question 1:
Considering the following table structures in database (ignore the absence of PRIMARY Key
constraints):
TABLE customers
id INTEGER,
name VARCHAR(50)
TABLE bookings
id INTEGER,
amount DECIMAL(10,2)
Objective: Write a query to select all customers together with number of bookings they made and
the sum of amount of all bookings by each of them. Customers with 0 bookings should be included
as 0 bookings and 0 sum.
Question 2:
TABLE city
id INTEGER,
name INTEGER
TABLE citizen
id INTEGER,
name VARCHAR(50)
Objective: Populate the table cityPopulation defined below with total population (number of total
citizens) of each city.
TABLE cityPopulation
cityName VARCHAR(50),
population INTEGER
Question 3:
TABLE team
id INTEGER,
name VARCHAR(50)
TABLE members
id INTEGER,
name VARCHAR(50),
TABLE results
mem_rank INTEGER,
year INTEGER
Objective: Write a query that returns the name of the team that has at least one member as winner
(ranking as 1) for year 2019 and 2020. It should return the name of the team, ranking of their best
members for 2019,2020 and number of members who were winners (ranking as 1).
Question 4:
TABLE companies
id INTEGER,
name VARCHAR(50)
TABLE employee
id INTEGER,
name VARCHAR(50),
TABLE competition
id INTEGER,
name VARCHAR(50)
TABLE comp_participant
Objective: Write a query that returns the name of the company and the number of the employees
participating in any competition.
Question 5:
TABLE cabinet
id INTEGER,
name VARCHAR(50),
supervisorId INTEGER
Each minister has either one supervisor or none. Supervisors can also have supervisors and hierarchy
can extend to multiple levels. Top-Level supervisors have no supervisors above them (supervisorId in
null). All ministers whose supervisorId is null are top-level ministers regardless of whether they have
ministers under them or not.
* Top-level supervisors id
Any top-level supervisor who have no employees under them should return 0 as the ministers under
them.
Question 6:
TABLE matchScore
playerId INTEGER,
playerName VARCHAR(50),
runs INTEGER,
matchId INTEGER
Objective: Write a query using SQL analytics functions that returns the result with PlayerName, runs
and matchId of all players who scored highest in the respective matches.
Question 7:
TABLE city
id INTEGER,
name INTEGER
TABLE customers
id INTEGER,
name VARCHAR(50),
orders_count INTEGER
Objective: Using a subquery, delete the customers who belong to city ‘LONDON’ and has 0 (zero)
orders so far.
Question 8:
TABLE city
id INTEGER,
name VARCHAR(50)
TABLE citizen
id INTEGER,
name VARCHAR(50),
income INTEGER,
1. City Name
2. Threshold income for the poverty line in the city (average income of the city). City with no citizens
should return 1000 as threshold income
* 'Above' when city’s average income is greater than the overall average income (average
income of all citizens)
* 'Below' when city’s average income is less than the overall average income (average
income of all citizens)
* 'Equal' when city’s average income is equal as the overall average income (average income
of all citizens)
Question 9:
Information about employees and their manager are stored in the following table:
TABLE employees
id INTEGER,
name VARCHAR(50),
durationInCompany INTEGER
Objective: Write a query that selects the names of the managers together with the duration
of the oldest employee under them.
Question 10:
TABLE strengths
id INTEGER,
name VARCHAR(50)
TABLE players
id INTEGER,
name VARCHAR(50)
TABLE playerStrength
Objective: Write a query to return the players that have more than one strength and none
of the strengths are 'Running'.
* Player name