Last active
January 24, 2024 08:23
-
-
Save mindyng/028a84855cb05f27d6a8feb1b42df744 to your computer and use it in GitHub Desktop.
TestDome SQL Practice Q's
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*App usage data are kept in the following table: | |
TABLE sessions | |
id INTEGER PRIMARY KEY, | |
userId INTEGER NOT NULL, | |
duration DECIMAL NOT NULL | |
Write a query that selects userId and average session duration for each user who has more than one session.*/ | |
-- Example case create statement: | |
CREATE TABLE sessions ( | |
id INTEGER NOT NULL PRIMARY KEY, | |
userId INTEGER NOT NULL, | |
duration DECIMAL NOT NULL | |
); | |
INSERT INTO sessions(id, userId, duration) VALUES(1, 1, 10); | |
INSERT INTO sessions(id, userId, duration) VALUES(2, 2, 18); | |
INSERT INTO sessions(id, userId, duration) VALUES(3, 1, 14); | |
-- Expected output: | |
-- UserId AverageDuration | |
-- ----------------------- | |
-- 1 12 | |
with CTE as ( | |
select userid, count(*) as userid_count, avg(duration) AverageDuration | |
from sessions | |
group by 1 | |
having userid_count>1 | |
) | |
select userid, AverageDuration | |
from CTE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Given the following data definition, write a query that returns the number of students whose first name is John. String comparisons should be case sensitive. | |
TABLE students | |
id INTEGER PRIMARY KEY, | |
firstName VARCHAR(30) NOT NULL, | |
lastName VARCHAR(30) NOT NULL*/ | |
select count(*) | |
from students | |
where firstName = 'John' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*A table containing the students enrolled in a yearly course has incorrect data in records with ids between 20 and 100 (inclusive). | |
TABLE enrollments | |
id INTEGER NOT NULL PRIMARY KEY | |
year INTEGER NOT NULL | |
studentId INTEGER NOT NULL | |
Write a query that updates the field 'year' of every faulty record to 2015.*/ | |
update enrollments | |
set year = 2015 | |
where id between 20 and 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*The following data definition defines an organization's employee hierarchy. | |
An employee is a manager if any other employee has their managerId set to this employee's id. That means John is a manager if at least one other employee has their managerId set to John's id. | |
TABLE employees | |
id INTEGER NOT NULL PRIMARY KEY | |
managerId INTEGER REFERENCES employees(id) | |
name VARCHAR(30) NOT NULL | |
Write a query that selects only the names of employees who are not managers.*/ | |
select name | |
from employees | |
where id not in (select managerid from employees where managerid is Not NULL) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Information about pets is kept in two separate tables: | |
TABLE dogs | |
id INTEGER NOT NULL PRIMARY KEY, | |
name VARCHAR(50) NOT NULL | |
TABLE cats | |
id INTEGER NOT NULL PRIMARY KEY, | |
name VARCHAR(50) NOT NULL | |
Write a query that select all distinct pet names.*/ | |
select name from dogs | |
union | |
select name from cats |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*An insurance company maintains records of sales made by its employees. Each employee is assigned to a state. States are grouped under regions. The following tables contain the data: | |
TABLE regions | |
id INTEGER PRIMARY KEY | |
name VARCHAR(50) NOT NULL | |
TABLE states | |
id INTEGER PRIMARY KEY | |
name VARCHAR(50) NOT NULL | |
regionId INTEGER NOT NULL REFERENCES regions(id) | |
TABLE employees | |
id INTEGER PRIMARY KEY | |
name VARCHAR(50) NOT NULL | |
stateId INTEGER NOT NULL REFERENCES states(id) | |
TABLE sales | |
id INTEGER PRIMARY KEY | |
amount INTEGER NOT NULL | |
employeeId INTEGER NOT NULL REFERENCES employees(id) | |
Management requires a comparative region sales analysis report. | |
Write a query that returns: | |
1. The region name. | |
2. Average sales per employee for the region (Average sales = Total sales made for the region / Number of employees in the region). | |
3. The difference between the average sales of the region with the highest average sales, and the average sales per employee for the region (average sales to be calculated as explained above). | |
Employees can have multiple sales. A region with no sales should be also returned. Use 0 for average sales per employee for such a region when calculating the 2nd and the 3rd column. | |
Tags: SQL AGGREGATION LEFT JOIN SELECT PUBLIC | |
-- Example case create statement: | |
CREATE TABLE regions( | |
id INTEGER PRIMARY KEY, | |
name VARCHAR(50) NOT NULL | |
); | |
CREATE TABLE states( | |
id INTEGER PRIMARY KEY, | |
name VARCHAR(50) NOT NULL, | |
regionId INTEGER NOT NULL REFERENCES regions(id) | |
); | |
CREATE TABLE employees ( | |
id INTEGER PRIMARY KEY, | |
name VARCHAR(50) NOT NULL, | |
stateId INTEGER NOT NULL REFERENCES states(id) | |
); | |
CREATE TABLE sales ( | |
id INTEGER PRIMARY KEY, | |
amount INTEGER NOT NULL, | |
employeeId INTEGER NOT NULL REFERENCES employees(id) | |
); | |
INSERT INTO regions(id, name) VALUES(1, 'North'); | |
INSERT INTO regions(id, name) VALUES(2, 'South'); | |
INSERT INTO regions(id, name) VALUES(3, 'East'); | |
INSERT INTO regions(id, name) VALUES(4, 'West'); | |
INSERT INTO regions(id, name) VALUES(5, 'Midwest'); | |
INSERT INTO states(id, name, regionId) VALUES(1, 'Minnesota', 1); | |
INSERT INTO states(id, name, regionId) VALUES(2, 'Texas', 2); | |
INSERT INTO states(id, name, regionId) VALUES(3, 'California', 3); | |
INSERT INTO states(id, name, regionId) VALUES(4, 'Columbia', 4); | |
INSERT INTO states(id, name, regionId) VALUES(5, 'Indiana', 5); | |
INSERT INTO employees(id, name, stateId) VALUES(1, 'Jaden', 1); | |
INSERT INTO employees(id, name, stateId) VALUES(2, 'Abby', 1); | |
INSERT INTO employees(id, name, stateId) VALUES(3, 'Amaya', 2); | |
INSERT INTO employees(id, name, stateId) VALUES(4, 'Robert', 3); | |
INSERT INTO employees(id, name, stateId) VALUES(5, 'Tom', 4); | |
INSERT INTO employees(id, name, stateId) VALUES(6, 'William', 5); | |
INSERT INTO sales(id, amount, employeeId) VALUES(1, 2000, 1); | |
INSERT INTO sales(id, amount, employeeId) VALUES(2, 3000, 2); | |
INSERT INTO sales(id, amount, employeeId) VALUES(3, 4000, 3); | |
INSERT INTO sales(id, amount, employeeId) VALUES(4, 1200, 4); | |
INSERT INTO sales(id, amount, employeeId) VALUES(5, 2400, 5); | |
-- e.g. 'Minnesota' is the only state under the 'North' region. | |
-- Total sales made by employees 'Jaden' and 'Abby' for the state of 'Minnesota' is 5000 (2000 + 3000) | |
-- Total employees in the state of 'Minnesota' is 2 | |
-- Average sales per employee for the 'North' region = Total sales made for the region (5000) / Total number of employees (2) = 2500 | |
-- Difference between the average sales of the region with the highest average sales ('South'), | |
-- and the average sales per employee for the region ('North') = 4000 - 2500 = 1500. | |
-- Similarly, no sale has been made for the only state 'Indiana' under the region 'Midwest'. | |
-- So the average sales per employee for the region is 0. | |
-- And, the difference between the average sales of the region with the highest average sales ('South'), | |
-- and the average sales per employee for the region ('Midwest') = 4000 - 0 = 4000. | |
-- Expected output (rows in any order): | |
-- name average difference | |
-- ----------------------------- | |
-- North 2500 1500 | |
-- South 4000 0 | |
-- East 1200 2800 | |
-- West 2400 1600 | |
-- Midwest 0 4000 | |
*/ | |
with sales_avg AS ( | |
select r.name as region, | |
case when sum(ifnull(s.amount,0)) = 0 then 0 | |
else sum(ifnull(s.amount,0))/count(distinct e.id) end as average | |
from regions r | |
left join states st on r.id = st.regionid | |
left join employees e on st.id = e.stateid | |
left join sales s on e.id = s.employeeid | |
group by r.id, r.name) | |
select region, average, (select max(average) from sales_avg) - average as difference | |
from sales_avg | |
group by 1 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Each item in a web shop belongs to a seller. To ensure service quality, each seller has a rating. | |
The data are kept in the following two tables: | |
TABLE sellers | |
id INTEGER PRIMARY KEY, | |
name VARCHAR(30) NOT NULL, | |
rating INTEGER NOT NULL | |
TABLE items | |
id INTEGER PRIMARY KEY, | |
name VARCHAR(30) NOT NULL, | |
sellerId INTEGER REFERENCES sellers(id) | |
Write a query that selects the item name and the name of its seller for each item that belongs to a seller with a rating greater than 4. The query should return the name of the item as the first column and name of the seller as the second column.*/ | |
select i.name, s.name | |
from sellers s | |
join items i | |
on s.id = i.sellerId | |
where rating >4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*The following two tables are used to define users and their respective roles: | |
TABLE users | |
id INTEGER NOT NULL PRIMARY KEY, | |
userName VARCHAR(50) NOT NULL | |
TABLE roles | |
id INTEGER NOT NULL PRIMARY KEY, | |
role VARCHAR(20) NOT NULL | |
The users_roles table should contain the mapping between each user and their roles. Each user can have many roles, and each role can have many users. | |
Modify the provided SQLite create table statement so that: | |
Only users from the users table can exist within users_roles. | |
Only roles from the roles table can exist within users_roles. | |
A user can only have a specific role once. | |
Tags: constraints, create table */ | |
CREATE TABLE users_roles ( | |
userId INTEGER NOT NULL, | |
roleId INTEGER NOT NULL, | |
FOREIGN KEY(userId) REFERENCES users(id), | |
FOREIGN KEY(roleId) REFERENCES roles(id), | |
PRIMARY KEY (userId, roleId) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you. This was very helpful :)