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 thename
andsalary
columns.FROM employees
: Specifies theemployees
table.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 thename
column.FROM employees
: Specifies theemployees
table.ORDER BY hire_date ASC
: Sorts the results by thehire_date
column 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 theemployees
table.
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 thedepartment
column and counts the number of employees in each department, labeling the result asemployee_count
.FROM employees
: Specifies theemployees
table.GROUP BY department
: Groups the results by thedepartment
column.
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 thename
column from bothemployees
anddepartments
tables and labels them asemployee_name
anddepartment_name
respectively.FROM employees
: Specifies theemployees
table.JOIN departments ON employees.department_id = departments.id
: Joins theemployees
table with thedepartments
table on thedepartment_id
column.
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 thename
column.FROM employees
: Specifies theemployees
table.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 theemployees
table to update.SET salary = salary * 1.10
: Increases thesalary
column 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 theemployees
table to delete from.WHERE last_active_date < '2020-01-01'
: Deletes employees whoselast_active_date
is 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