Queries For Practice in SQL
Sure! Here are some practice SQL queries covering various important concepts, so you can sharpen your skills:
Sample Table: employees
| employee_id | first_name | last_name | department | salary | hire_date |
|---|---|---|---|---|---|
| 1 | John | Doe | Sales | 50000 | 2019-01-15 |
| 2 | Jane | Smith | HR | 60000 | 2018-03-12 |
| 3 | Alice | Johnson | Sales | 55000 | 2020-07-08 |
| 4 | Bob | Brown | IT | 70000 | 2017-10-20 |
| 5 | Carol | Davis | IT | 72000 | 2021-01-30 |
Practice Queries
1. Select all columns from employees
SELECT * FROM employees;2. Select first and last names of employees in the Sales department
SELECT first_name, last_nameFROM employeesWHERE department = 'Sales';3. Find employees with salary greater than 60000
SELECT *FROM employeesWHERE salary > 60000;4. Count number of employees in each department
SELECT department, COUNT(*) AS employee_countFROM employeesGROUP BY department;5. Find the highest salary in the company
SELECT MAX(salary) AS highest_salaryFROM employees;6. List employees ordered by hire_date ascending
SELECT *FROM employeesORDER BY hire_date ASC;7. Update salary of employee with employee_id = 3 to 58000
UPDATE employeesSET salary = 58000WHERE employee_id = 3;8. Delete employee with employee_id = 5
DELETE FROM employeesWHERE employee_id = 5;9. Insert a new employee record
INSERT INTO employees (employee_id, first_name, last_name, department, salary, hire_date)VALUES (6, 'Eve', 'Wilson', 'HR', 62000, '2023-05-15');10. Find employees hired after January 1, 2019
SELECT *FROM employeesWHERE hire_date > '2019-01-01';Bonus Advanced Queries:
Join employees with a
departmentstable to get department namesFind average salary per department
Find employees whose names start with 'J'
Use
LIKEto find employees with ‘son’ in last name
Would you like me to create sample data for you to try these queries? Or queries for any specific topic like JOINS, AGGREGATES, or SUBQUERIES?