SQL Review Worksheet
SQL Review Worksheet
SQL Overview
1. 1. What does SQL stand for, and what is its primary purpose?
- Structured Query Language
- Its primary purpose is to manage and manipulate relational databases
SQL Environment
3. 1. What are the three main components of SQL?
- Database Manipulation Language – DML
+ Commands that manage and query database
- Database control language – DCL
+ Commands that control database including administering privileges and committing
data
-Database Definxition Language – DDL
+ Commands that define a database, including creating, altering and dropping table and
establishing constraints
SELECT Statement
5. 1. Write a SQL query to retrieve all columns from a table named 'Employees'.
Select * from Employees
6. 2. Write a SQL query to sort employees by their last names in ascending order.
Select * from employees
Order by employee_lastname asc;
7. 3. How does the WHERE clause differ from the HAVING clause? Provide an example for
each.
WHERE: Filters rows before grouping.
HAVING: Filters groups after aggregation.
Examples:
WHERE: SELECT * FROM Employees
WHERE Department = 'Sales';
HAVING:
o SELECT Department, COUNT(*) FROM Employees
GROUP BY Department
HAVING COUNT(*) > 5;
Aggregation and Grouping
8. 1. Write a SQL query to count the number of employees in each department.
- It groups rows with the same values in specified columns for aggregation.
10. 3. Provide an example of using the HAVING clause with a GROUP BY.
Another example:
SELECT Department, AVG(Salary) AS AvgSalary
FROM Employees
GROUP BY Department
HAVING AVG(Salary) > 50000;
Joins
11. 1. Write a SQL query using an INNER JOIN to combine data from 'Employees' and
'Departments' tables.
Select *
From Employees
Inner join departments
ON Employees.employee_id = departments.employee_id;
Another:
12. 2. What is the difference between an INNER JOIN and a LEFT OUTER JOIN?
Subqueries
14. 1. Write a SQL query to find employees with salaries higher than the average salary.
SELECT *
FROM employees
WHERE salaries > (SELECT AVG(salaries) FROM employess);
Compound Queries
17. 1. What is the difference between UNION and UNION ALL?
18. 2. Write a SQL query to combine customers and sales reps into a single list of names and
cities using UNION.
INSERT Statement
19. 1. Write a SQL query to insert a new row into the 'Customers' table.
20. 2. How can you insert data into a table from another table? Provide an example.
UPDATE Statement
21. 1. Write a SQL query to update the salary of an employee with ID 101 to $60,000.
UPDATE employee
SET salary = 60,000
WHERE ID = 101;
UPDATE Employees
SET Salary = Salary * 1.1
WHERE Department = 'Sales';
DELETE Statement
23. 1. Write a SQL query to delete all employees in the 'Sales' department.
24. 2. What happens if you omit the WHERE clause in a DELETE statement?
Date Functions
25. 1. Write a query to display the current date in MySQL.
SELECT CURDATE();
26. 2. How do you retrieve the current date and time in MS SQL Server?
SELECT GETDATE();
SELECT *
FROM Customers
WHERE City LIKE 'B%';
The number of columns and their data types must match in all SELECT statements.