SQL Exercises/The computer store
< SQL Exercises
Jump to navigationJump to search
Contents
1Relational Schema
2Exercises
3Table creation code
4Sample dataset
Relational Schema[edit | edit source]
Please note the datatypes given are SQLite datatypes.
PK and FK stand for primary key and foreign key respectively.
Exercises[edit | edit source]
1. Select the names of all the products in the store.
Click to see solution
SELECT Name FROM Products;
2. Select the names and the prices of all the products in the store.
Click to see solution
SELECT Name, Price FROM Products;
3. Select the name of the products with a price less than or equal to $200.
Click to see solution
SELECT Name FROM Products WHERE Price <= 200;
4. Select all the products with a price between $60 and $120.
Click to see solution
/* With AND */
SELECT * FROM Products
WHERE Price >= 60 AND Price <= 120;
/* With BETWEEN */
SELECT * FROM Products
WHERE Price BETWEEN 60 AND 120;
5. Select the name and price in cents (i.e., the price must be multiplied by 100).
Click to see solution
/* Without AS */
SELECT Name, Price * 100 FROM Products;
/* With AS */
SELECT Name, Price * 100 AS PriceCents FROM Products;
6. Compute the average price of all the products.
Click to see solution
SELECT AVG(Price) FROM Products;
7. Compute the average price of all products with manufacturer code equal to 2.
Click to see solution
SELECT AVG(Price) FROM Products WHERE Manufacturer=2;
8. Compute the number of products with a price larger than or equal to $180.
Click to see solution
SELECT COUNT(*) FROM Products WHERE Price >= 180;
9. Select the name and price of all products with a price larger than or equal to $180, and sort
first by price (in descending order), and then by name (in ascending order).
Click to see solution
SELECT Name, Price
FROM Products
WHERE Price >= 180
ORDER BY Price DESC, Name;
10. Select all the data from the products, including all the data for each product's manufacturer.
Click to see solution
/* Without LEFT JOIN */
SELECT * FROM Products, Manufacturers
WHERE Products.Manufacturer = Manufacturers.Code;
/* With LEFT JOIN */
SELECT *
FROM Products LEFT JOIN Manufacturers
ON Products.Manufacturer = Manufacturers.Code;
11. Select the product name, price, and manufacturer name of all the products.
Click to see solution
/* Without INNER JOIN */
SELECT Products.Name, Price, Manufacturers.Name
FROM Products, Manufacturers
WHERE Products.Manufacturer = Manufacturers.Code;
/* With INNER JOIN */
SELECT Products.Name, Price, Manufacturers.Name
FROM Products INNER JOIN Manufacturers
ON Products.Manufacturer = Manufacturers.Code;
12. Select the average price of each manufacturer's products, showing only the manufacturer's
code.
Click to see solution
SELECT AVG(Price), Manufacturer
FROM Products
GROUP BY Manufacturer;
13. Select the average price of each manufacturer's products, showing the manufacturer's
name.
Click to see solution
/* Without INNER JOIN */
SELECT AVG(Price), Manufacturers.Name
FROM Products, Manufacturers
WHERE Products.Manufacturer = Manufacturers.Code
GROUP BY Manufacturers.Name;
/* With INNER JOIN */
SELECT AVG(Price), Manufacturers.Name
FROM Products INNER JOIN Manufacturers
ON Products.Manufacturer = Manufacturers.Code
GROUP BY Manufacturers.Name;
14. Select the names of manufacturer whose products have an average price larger than or
equal to $150.
Click to see solution
/* Without INNER JOIN */
SELECT AVG(Price), Manufacturers.Name
FROM Products, Manufacturers
WHERE Products.Manufacturer = Manufacturers.Code
GROUP BY Manufacturers.Name
HAVING AVG(Price) >= 150;
/* With INNER JOIN */
SELECT AVG(Price), Manufacturers.Name
FROM Products INNER JOIN Manufacturers
ON Products.Manufacturer = Manufacturers.Code
GROUP BY Manufacturers.Name
HAVING AVG(Price) >= 150;
15. Select the name and price of the cheapest product.
Click to see solution
SELECT name,price
FROM Products
ORDER BY price ASC
LIMIT 1
/* With a nested SELECT */
/* WARNING: If there is more than one item with the cheapest price it
will select them both */
SELECT Name, Price
FROM Products
WHERE Price = (SELECT MIN(Price) FROM Products);
16. Select the name of each manufacturer along with the name and price of its most expensive
product.
Click to see solution
/* With a nested SELECT and without INNER JOIN */
SELECT A.Name, A.Price, F.Name
FROM Products A, Manufacturers F
WHERE A.Manufacturer = F.Code
AND A.Price =
(
SELECT MAX(A.Price)
FROM Products A
WHERE A.Manufacturer = F.Code
);
/* With a nested SELECT and an INNER JOIN */
SELECT A.Name, A.Price, F.Name
FROM Products A INNER JOIN Manufacturers F
ON A.Manufacturer = F.Code
AND A.Price =
(
SELECT MAX(A.Price)
FROM Products A
WHERE A.Manufacturer = F.Code
);
17. Select the name of each manufacturer which have an average price above $145 and
contain at least 2 different products.
Click to see solution
Select m.Name, Avg(p.price) as p_price, COUNT(p.Manufacturer) as m_count
FROM Manufacturers m, Products p
WHERE p.Manufacturer = m.code
GROUP BY m.Name , p.Manufacturer
HAVING Avg(p.price) >= 150 and COUNT(p.Manufacturer) >= 2;
18. Add a new product: Loudspeakers, $70, manufacturer 2.
Click to see solution
INSERT INTO Products( Code, Name , Price , Manufacturer)
VALUES ( 11, 'Loudspeakers' , 70 , 2 );
19. Update the name of product 8 to "Laser Printer".
Click to see solution
UPDATE Products
SET Name = 'Laser Printer'
WHERE Code = 8;
20. Apply a 10% discount to all products.
Click to see solution
UPDATE Products
SET Price = Price - (Price * 0.1);
Table creation code[edit | edit source]
CREATE TABLE Manufacturers (
Code INTEGER PRIMARY KEY NOT NULL,
Name CHAR(50) NOT NULL
);
CREATE TABLE Products (
Code INTEGER PRIMARY KEY NOT NULL,
Name CHAR(50) NOT NULL ,
Price REAL NOT NULL ,
Manufacturer INTEGER NOT NULL
CONSTRAINT fk_Manufacturers_Code REFERENCES
Manufacturers(Code)
);
Please note the syntax presented here is for the SQLite system. It has been tested by the
authors on sqlite3 specifically.
The code is also tested against SQL Server 2017.
Also note that the NOT NULL constraint on the primary key fields is semantically redundant,
but a syntactic necessity in SQLite.
Click to see MySQL syntax.
CREATE TABLE Manufacturers (
Code INTEGER,
Name VARCHAR(255) NOT NULL,
PRIMARY KEY (Code)
);
CREATE TABLE Products (
Code INTEGER,
Name VARCHAR(255) NOT NULL ,
Price DECIMAL NOT NULL ,
Manufacturer INTEGER NOT NULL,
PRIMARY KEY (Code),
FOREIGN KEY (Manufacturer) REFERENCES Manufacturers(Code)
) ENGINE=INNODB;
Sample dataset[edit | edit source]
INSERT INTO Manufacturers(Code,Name) VALUES (1,'Sony'),(2,'Creative
Labs'),(3,'Hewlett-Packard'),(4,'Iomega'),(5,'Fujitsu'),
(6,'Winchester');
INSERT INTO Products(Code,Name,Price,Manufacturer) VALUES(1,'Hard
drive',240,5),(2,'Memory',120,6),(3,'ZIP drive',150,4),(4,'Floppy
disk',5,6),(5,'Monitor',240,1),
(6,'DVD drive',180,2),(7,'CD drive',90,2),(8,'Printer',270,3),
(9,'Toner cartridge',66,3),(10,'DVD burner',180,2);
SQL Exercises/Employee management
< SQL Exercises
Jump to navigationJump to search
Contents
1Relational Schema
2Exercises
3Table creation code
4Sample dataset
Relational Schema[edit | edit source]
Exercises[edit | edit source]
1. Select the last name of all employees.
Click to see solution
SELECT LastName FROM Employees;
2. Select the last name of all employees, without duplicates.
Click to see solution
SELECT DISTINCT LastName FROM Employees;
3. Select all the data of employees whose last name is "Smith".
Click to see solution
SELECT * FROM Employees WHERE LastName = 'Smith';
4. Select all the data of employees whose last name is "Smith" or "Doe".
Click to see solution
/* With OR */
SELECT * FROM Employees
WHERE LastName = 'Smith' OR LastName = 'Doe';
/* With IN */
SELECT * FROM Employees
WHERE LastName IN ('Smith' , 'Doe');
5. Select all the data of employees that work in department 14.
Click to see solution
SELECT * FROM Employees WHERE Department = 14;
6. Select all the data of employees that work in department 37 or department 77.
Click to see solution
/* With OR */
SELECT * FROM Employees
WHERE Department = 37 OR Department = 77;
/* With IN */
SELECT * FROM Employees
WHERE Department IN (37,77);
7. Select all the data of employees whose last name begins with an "S".
Click to see solution
SELECT * FROM Employees
WHERE LastName LIKE 'S%';
8. Select the sum of all the departments' budgets.
Click to see solution
SELECT SUM(Budget) FROM Departments;
9. Select the number of employees in each department (you only need to show the department
code and the number of employees).
Click to see solution
SELECT Department, COUNT(*)
FROM Employees
GROUP BY Department;
10. Select all the data of employees, including each employee's department's data.
Click to see solution
SELECT *
FROM Employees E INNER JOIN Departments D
ON E.Department = D.Code;
11. Select the name and last name of each employee, along with the name and budget of the
employee's department.
Click to see solution
/* Without labels */
SELECT Employees.Name, LastName, Departments.Name AS DepartmentsName,
Budget
FROM Employees INNER JOIN Departments
ON Employees.Department = Departments.Code;
/* With labels */
SELECT E.Name, LastName, D.Name AS DepartmentsName, Budget
FROM Employees E INNER JOIN Departments D
ON E.Department = D.Code;
12. Select the name and last name of employees working for departments with a budget
greater than $60,000.
Click to see solution
/* Without subquery */
SELECT Employees.Name, LastName
FROM Employees INNER JOIN Departments
ON Employees.Department = Departments.Code
AND Departments.Budget > 60000;
/* With subquery */
SELECT Name, LastName FROM Employees
WHERE Department IN
(SELECT Code FROM Departments WHERE Budget > 60000);
13. Select the departments with a budget larger than the average budget of all the
departments.
Click to see solution
SELECT *
FROM Departments
WHERE Budget >
(
SELECT AVG(Budget)
FROM Departments
);
14. Select the names of departments with more than two employees.
Click to see solution
/*With subquery*/
SELECT D.Name FROM Departments D
WHERE 2 <
(
SELECT COUNT(*)
FROM Employees
WHERE Department = D.Code
);
/* With IN and subquery */
SELECT Name FROM Departments
WHERE Code IN
(
SELECT Department
FROM Employees
GROUP BY Department
HAVING COUNT(*) > 2
);
/* With UNION. This assumes that no two departments have
the same name */
SELECT Departments.Name
FROM Employees INNER JOIN Departments
ON Department = Code
GROUP BY Departments.Name
HAVING COUNT(*) > 2;
15. Select the name and last name of employees working for departments with second lowest
budget.
Click to see solution
/* With subquery */
SELECT e.Name, e.LastName
FROM Employees e
WHERE e.Department = (
SELECT sub.Code
FROM (SELECT * FROM Departments d ORDER BY d.budget LIMIT 2) sub
ORDER BY budget DESC LIMIT 1);
/* With subquery */
SELECT Name, LastName
FROM Employees
WHERE Department IN (
SELECT Code
FROM Departments
WHERE Budget = (
SELECT TOP 1 Budget
FROM Departments
WHERE Budget IN (
SELECT DISTINCT TOP 2 Budget
FROM Departments
ORDER BY Budget ASC
)
ORDER BY Budget DESC
)
);
16. Add a new department called "Quality Assurance", with a budget of $40,000 and
departmental code 11. Add an employee called "Mary Moore" in that department, with SSN
847-21-9811.
Click to see solution
INSERT INTO Departments
VALUES ( 11 , 'Quality Assurance' , 40000);
INSERT INTO Employees
VALUES ( '847219811' , 'Mary' , 'Moore' , 11);
/*Note: Quoting numbers in SQL works but is bad practice. SSN should not be quoted it is an
integer.*/
17. Reduce the budget of all departments by 10%.
Click to see solution
UPDATE Departments SET Budget = Budget * 0.9;
18. Reassign all employees from the Research department (code 77) to the IT department
(code 14).
Click to see solution
UPDATE Employees SET Department = 14 WHERE Department = 77;
19. Delete from the table all employees in the IT department (code 14).
Click to see solution
DELETE FROM Employees
WHERE Department = 14;
20. Delete from the table all employees who work in departments with a budget greater than or
equal to $60,000.
Click to see solution
DELETE FROM Employees
WHERE Department IN
(
SELECT Code FROM Departments
WHERE Budget >= 60000
);
21. Delete from the table all employees.
Click to see solution
DELETE FROM Employees;
Table creation code[edit | edit source]
CREATE TABLE Departments (
Code INTEGER PRIMARY KEY NOT NULL,
Name VARCHAR NOT NULL ,
Budget REAL NOT NULL
);
CREATE TABLE Employees (
SSN INTEGER PRIMARY KEY NOT NULL,
Name TEXT NOT NULL ,
LastName VARCHAR NOT NULL ,--since question 2 asks about removing
duplicate - text must be converted if the answer is using distinct
Department INTEGER NOT NULL ,
CONSTRAINT fk_Departments_Code FOREIGN KEY(Department)
REFERENCES Departments(Code)
);
Click to see MySQL syntax.
Click to see Oracle syntax.
CREATE TABLE Departments (
Code INTEGER PRIMARY KEY,
Name varchar(255) NOT NULL ,
Budget decimal NOT NULL
);
CREATE TABLE Employees (
SSN INTEGER PRIMARY KEY,
Name varchar(255) NOT NULL ,
LastName varchar(255) NOT NULL ,
Department INTEGER NOT NULL ,
foreign key (department) references Departments(Code)
) ENGINE=INNODB;
Sample dataset[edit | edit source]
INSERT INTO Departments(Code,Name,Budget) VALUES(14,'IT',65000);
INSERT INTO Departments(Code,Name,Budget) VALUES(37,'Accounting',15000);
INSERT INTO Departments(Code,Name,Budget) VALUES(59,'Human
Resources',240000);
INSERT INTO Departments(Code,Name,Budget) VALUES(77,'Research',55000);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('123234877','Michael','Rogers',14);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('152934485','Anand','Manikutty',14);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('222364883','Carol','Smith',37);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('326587417','Joe','Stevens',37);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('332154719','Mary-Anne','Foster',14);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('332569843','George','O''Donnell',77);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('546523478','John','Doe',59);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('631231482','David','Smith',77);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('654873219','Zacary','Efron',59);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('745685214','Eric','Goldsmith',59);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('845657245','Elizabeth','Doe',14);
INSERT INTO Employees(SSN,Name,LastName,Department)
VALUES('845657246','Kumar','Swamy',14);