![]() |
VOOZH | about |
Structured Query Language (SQL) is an essential skill for data analysts which enables them to extract, manipulate and analyze data efficiently. Regular practice with SQL exercises helps improve query-writing skills, enhances understanding of database structures, and builds expertise in using aggregation functions, joins, subqueries, and performance optimization techniques.
By working through beginner, intermediate, and advanced SQL exercises, analysts can strengthen their ability to handle real-world data challenges and make informed decisions based on data insights. In this article, We will learn about the SQL exercise which helps you to get more insight by performing the SQL scripts and so on.
Data analysts rely on SQL to extract insights from databases efficiently. Regular practice with SQL exercises enhances proficiency in:
Practicing SQL with beginner-friendly exercises helps build a strong foundation in database querying. Start with basic queries like retrieving all records using SELECT *, selecting specific columns, and filtering data with WHERE. Learn to sort results using ORDER BY and apply aggregate functions like COUNT(*) with GROUP BY. These exercises enhance data manipulation skills and prepare beginners for more advanced SQL concepts.
SELECT * FROM employees;Output:
Explanation: This query retrieves all records from the "employees" table, displaying every column for each row.
SELECT first_name, last_name FROM employees;Output:
Explanation: This query selects only the "first_name" and "last_name" columns from the "employees" table.
SELECT * FROM employees WHERE department = 'Sales';Output:
Explanation: This query filters and retrieves only the employees who belong to the "Sales" department.
SELECT * FROM employees ORDER BY salary DESC;Output:
Explanation: This query sorts employees in descending order based on their salary.
SELECT department, COUNT(*) FROM employees GROUP BY department;Output:
Explanation: This query groups employees by department and counts the number of employees in each department.
Enhancing SQL skills involves practicing more advanced queries, such as filtering employees with salaries above the company average using subqueries, finding employees with the same manager, and performing table joins to retrieve related data. Learning to determine the second highest salary with nested queries and extracting employees hired within the last five years strengthens analytical abilities. These exercises help users master SQL for real-world data management.
SELECT * FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);Output:
Explanation: This query retrieves employees whose salary is higher than the average salary in the company.
SELECT * FROM employees WHERE manager_id = 101;Output:
Explanation: This query selects employees who report to the manager with ID 101.
SELECT employees.first_name, departments.department_name
FROM employees
JOIN departments ON employees.department_id = departments.department_id;
Output:
Explanation: This query joins the "employees" and "departments" tables on "department_id" to get each employee's department name.
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);Output:
Explanation:
SELECT * FROM employees WHERE hire_date > DATE_SUB(CURDATE(), INTERVAL 5 YEAR);Output:
Explanation: This query selects employees who were hired within the last five years.
Mastering SQL involves handling complex queries like identifying employees with multiple job roles, calculating running salary totals within departments, and using recursive queries for hierarchical data. Detecting duplicate records with GROUP BY and HAVING, as well as optimizing queries with indexes, enhances performance and efficiency.
These exercises develop advanced SQL skills for handling large datasets, improving query execution speed, and managing structured data effectively.
SELECT employee_id, first_name, last_name, salary, department_id
FROM employees e
WHERE salary > (SELECT AVG(salary)
FROM employees
WHERE department_id = e.department_id);
Output:
Explanation: This query selects employees whose salary is higher than the average salary of their respective department. It uses a correlated subquery to calculate the department's average salary and compares each employeeโs salary with that value.
SELECT employee_id, first_name, last_name, department_id, salary
FROM (SELECT employee_id, first_name, last_name, department_id, salary,
DENSE_RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rnk
FROM employees) ranked
WHERE rnk <= 2;
Output:
Explanation: This query uses the DENSE_RANK() function to assign a rank to employees based on their salary within each department (PARTITION BY department_id). The outer query filters for only the top 2 highest-paid employees in each department.
SELECT employee_id, first_name, last_name, department_id, hire_date
FROM employees e
WHERE hire_date < (SELECT AVG(hire_date)
FROM employees
WHERE department_id = e.department_id);
Output:
Explanation: This query selects employees who were hired before the average hire date of their department. The correlated subquery calculates the department-wise average hire date, and employees with an earlier hire date are considered more experienced.
SELECT e.employee_id, e.first_name, e.last_name, e.salary, e.manager_id
FROM employees e
JOIN employees m ON e.manager_id = m.employee_id
WHERE e.salary > m.salary;
Output:
Explanation: This query joins the employees table to itself (self-join) to compare each employee's salary with their managerโs salary. It returns employees who earn more than their manager.
CREATE INDEX idx_employee_salary ON employees(salary);Explanation: This command creates an index on the "salary" column to improve query performance when filtering or sorting by salary.
To maximize the benefits of SQL exercises, follow these best practices:
Mastering SQL requires consistent practice with various query types, from simple data retrieval to complex analytical queries. By engaging in structured SQL exercises, data analysts can develop a deep understanding of database operations, improve their problem-solving skills, and optimize query performance. Practicing SQL in real-world scenarios, experimenting with different queries, and applying indexing techniques can significantly enhance efficiency. As SQL remains a critical tool in data analysis, continuous learning and hands-on practice will help analysts stay proficient and competitive in the field.