In this section, we will provide practical exercises to help you reinforce your understanding of SQL. These exercises will cover various aspects of SQL, including basic operations, complex queries, and data manipulation. Each exercise will be followed by a detailed solution to help you understand the correct approach.
Exercise 1: Basic SELECT Queries
Task:
Write a SQL query to retrieve all columns from the employees table.
Solution:
Explanation:
The SELECT * statement is used to select all columns from the employees table.
Exercise 2: Filtering Data
Task:
Write a SQL query to retrieve the names and salaries of employees who earn more than $50,000.
Solution:
Explanation:
SELECT name, salary: Selects thenameandsalarycolumns.FROM employees: Specifies theemployeestable.WHERE salary > 50000: Filters the results to include only those employees whose salary is greater than $50,000.
Exercise 3: Sorting Data
Task:
Write a SQL query to retrieve the names of employees, sorted by their hire date in ascending order.
Solution:
Explanation:
SELECT name: Selects thenamecolumn.FROM employees: Specifies theemployeestable.ORDER BY hire_date ASC: Sorts the results by thehire_datecolumn in ascending order.
Exercise 4: Aggregating Data
Task:
Write a SQL query to find the average salary of all employees.
Solution:
Explanation:
SELECT AVG(salary) AS average_salary: Calculates the average salary and labels the result asaverage_salary.FROM employees: Specifies theemployeestable.
Exercise 5: Grouping Data
Task:
Write a SQL query to count the number of employees in each department.
Solution:
Explanation:
SELECT department, COUNT(*) AS employee_count: Selects thedepartmentcolumn and counts the number of employees in each department, labeling the result asemployee_count.FROM employees: Specifies theemployeestable.GROUP BY department: Groups the results by thedepartmentcolumn.
Exercise 6: Joining Tables
Task:
Write a SQL query to retrieve the names of employees along with the names of their departments. Assume there are two tables: employees and departments. The employees table has a department_id column that references the id column in the departments table.
Solution:
SELECT employees.name AS employee_name, departments.name AS department_name FROM employees JOIN departments ON employees.department_id = departments.id;
Explanation:
SELECT employees.name AS employee_name, departments.name AS department_name: Selects thenamecolumn from bothemployeesanddepartmentstables and labels them asemployee_nameanddepartment_namerespectively.FROM employees: Specifies theemployeestable.JOIN departments ON employees.department_id = departments.id: Joins theemployeestable with thedepartmentstable on thedepartment_idcolumn.
Exercise 7: Subqueries
Task:
Write a SQL query to find the names of employees who earn more than the average salary.
Solution:
Explanation:
SELECT name: Selects thenamecolumn.FROM employees: Specifies theemployeestable.WHERE salary > (SELECT AVG(salary) FROM employees): Filters the results to include only those employees whose salary is greater than the average salary calculated by the subquery.
Exercise 8: Updating Data
Task:
Write a SQL query to increase the salary of all employees in the 'Sales' department by 10%.
Solution:
Explanation:
UPDATE employees: Specifies theemployeestable to update.SET salary = salary * 1.10: Increases thesalarycolumn by 10%.WHERE department = 'Sales': Applies the update only to employees in the 'Sales' department.
Exercise 9: Deleting Data
Task:
Write a SQL query to delete all employees who have not been active since 2019. Assume there is a column last_active_date in the employees table.
Solution:
Explanation:
DELETE FROM employees: Specifies theemployeestable to delete from.WHERE last_active_date < '2020-01-01': Deletes employees whoselast_active_dateis before January 1, 2020.
Conclusion
These exercises cover a range of SQL operations, from basic SELECT queries to more complex tasks like joins and subqueries. By practicing these exercises, you should gain a solid understanding of how to manipulate and query data using SQL. Remember to test your queries in a database environment to see the results and further solidify your learning.
Fundamentals of Databases
Module 1: Introduction to Databases
Module 2: Relational Databases
Module 3: Non-Relational Databases
- Introduction to NoSQL
- Types of NoSQL Databases
- Comparison between Relational and Non-Relational Databases
Module 4: Schema Design
- Principles of Schema Design
- Entity-Relationship (ER) Diagrams
- Transformation of ER Diagrams to Relational Schemas
