You are on page 1of 10

SQL Test

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,

custid INTEGER REFERENCES customers(id),

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,

cityid INTEGER REFERENCES city(id),

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),

teamid INTEGER REFERENCES team(id)

TABLE results

memberid INTEGER REFERENCES members(id),

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),

companyid INTEGER REFERENCES companies(id)

TABLE competition

id INTEGER,

name VARCHAR(50)

TABLE comp_participant

employeeid INTEGER REFERENCES employee(id),

competitionid INTEGER REFERENCES competition(id)

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.

Objective: Write a query that returns:

* Top-level supervisors id

* Total number of ministers under them

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),

cityid INTEGER REFERENCES city(id),

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,

cityid INTEGER REFERENCES city(id)

Objective: Write a query that returns following 3 columns:

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

3. A derived column 'Status' that can have one of the following:

* '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,

managerId INTEGER REFERENCES employees(id),

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:

Considering the below table structures:

TABLE strengths

id INTEGER,

name VARCHAR(50)

TABLE players

id INTEGER,

name VARCHAR(50)

TABLE playerStrength

strengthId INTEGER REFERENCES strengths(id),

playerId INTEGER REFERENCES players(id)

PRIMARY KEY(strengthId, playerId)

A player can have more than one strength.

Objective: Write a query to return the players that have more than one strength and none
of the strengths are 'Running'.

Query should return:

* Player name

* Number of strengths the player has

You might also like