0% found this document useful (0 votes)
4 views5 pages

SQL Review Worksheet

The document provides a comprehensive overview of SQL, including its definition, components, and various commands such as DDL, DML, and DCL. It covers SQL queries for data retrieval, aggregation, joins, subqueries, and data manipulation, along with examples for each. Additionally, it explains the differences between SQL operations like INNER JOIN and LEFT OUTER JOIN, as well as the use of UNION and UNION ALL.

Uploaded by

vnhtto
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
4 views5 pages

SQL Review Worksheet

The document provides a comprehensive overview of SQL, including its definition, components, and various commands such as DDL, DML, and DCL. It covers SQL queries for data retrieval, aggregation, joins, subqueries, and data manipulation, along with examples for each. Additionally, it explains the differences between SQL operations like INNER JOIN and LEFT OUTER JOIN, as well as the use of UNION and UNION ALL.

Uploaded by

vnhtto
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

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

2. 2. List two benefits of having a standardized SQL language.


- enhanced application portability
- reduced training cost

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

4. 2. Provide an example command for each component: DDL, DML, DCL.

- DDL: CREATE TABLE Countries (Country_ID INT, Country_Name VARCHAR(50));


- DML: SELECT * FROM Countries;
- DCL: GRANT SELECT ON Countries TO User;

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.

SELECT Department, COUNT(*) AS EmployeeCount


FROM Employees
GROUP BY Department;

9. 2. What is the purpose of the GROUP BY clause?

- 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.

Select class_ID, count(*) AS male_student


From school
Where gender =’male’
Group by class_id
Having count(*) > 10;

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:

SELECT Employees.Name, Departments.DepartmentName


FROM Employees
INNER JOIN Departments ON Employees.DepartmentID = Departments.ID;

12. 2. What is the difference between an INNER JOIN and a LEFT OUTER JOIN?

 INNER JOIN: Only returns matching rows from both tables.


 LEFT OUTER JOIN: Returns all rows from the left table, with NULL for non-
matching rows.
13. 3. Write a SQL query to retrieve all employees and their managers using a SELF JOIN.

SELECT a.employee_name, b.employee_name AS manager_name


FROM employees AS a
INNER JOIN employees AS b
ON a.manager_id = b.employee_id;

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

15. 2. Provide an example of using a subquery in the WHERE clause.

SELECT employee_name, employee_salaries


FROM employees
WHERE employee_ID IN (SELECT department_id FROM departments );

16. 3. What are the benefits of using subqueries?

 Simplifies complex queries by dividing them into manageable parts.


 Enhances modularity and readability.

Compound Queries
17. 1. What is the difference between UNION and UNION ALL?

 UNION removes duplicate rows.


 UNION ALL includes all rows, including duplicates.

18. 2. Write a SQL query to combine customers and sales reps into a single list of names and
cities using UNION.

SELECT name, city FROM Customers


UNION
SELECT name, city FROM SaleReports;

INSERT Statement
19. 1. Write a SQL query to insert a new row into the 'Customers' table.

INSERT INTO Customers(customer_ID, name, job ) VALUES (1, ‘frank’, ‘Manager);


INSERT INTO Customers (ID, Name, City) VALUES (101, 'Jane Doe', 'Toronto');

20. 2. How can you insert data into a table from another table? Provide an example.

INSERT INTO NewCustomers (ID, Name, City)


SELECT ID, Name, City
FROM OldCustomers;

INSERT INTO Customer_Backup (CustNum, CustName, City, CreditLimit)


SELECT CustNum, CustName, City, CreditLimit
FROM Customer
WHERE City = 'Brampton';

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;

22. 2. Can you update multiple rows in a single query? How?

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.

DELETE FROM department


WHERE employees =’Sales’;

24. 2. What happens if you omit the WHERE clause in a DELETE statement?

All rows in the table will be deleted.

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();

Sorting and Filtering


27. 1. Write a SQL query to retrieve all customers from cities starting with 'B'.

SELECT *
FROM Customers
WHERE City LIKE 'B%';

28. 2. What is the purpose of the ORDER BY clause? Provide an example.

The ORDER BY clause sorts the results of a query.

Example for ascending:


SELECT *
FROM customer
ORDER BY city ASC;

Example for descending:


SELECT *
FROM Customers
ORDER BY City DESC;

UNION and UNION ALL


29. 1. Write a SQL query to combine all product names and supplier names into a single list.

SELECT ProductName FROM Products


UNION
SELECT SupplierName FROM Suppliers;

30. 2. What are the requirements for using UNION in SQL?

The number of columns and their data types must match in all SELECT statements.

You might also like