SQL Examples
SQL Examples
Here are practical SQL examples to demonstrate how various commands and concepts work:
1. SELECT Statement
Retrieve all rows from a table:
Retrieve specific columns:
2. SELECT DISTINCT
Retrieve unique job titles from the table:
3. WHERE Clause
Filter employees by department:
Find employees hired after 2020:
4. ORDER BY
Sort employees by last name in ascending order:
Sort by salary in descending order:
5. INSERT INTO
Add a new employee to the table:
6. UPDATE
Update the salary of an employee:
7. DELETE
Remove an employee from the table:
8. JOIN
Inner join to find employees with matching department details:
9. GROUP BY and HAVING
Count employees in each department:
Filter groups with more than 5 employees:
10. AGGREGATE FUNCTIONS
Find the average salary in the IT department:
Get the total salary expense for all employees:
11. LIKE and Wildcards
Find employees with first names starting with "J":
Find employees whose names contain "ohn":
12. IN Clause
Filter employees working in specific departments:
13. BETWEEN
Find employees with salaries between $50,000 and $70,000:
14. CASE
Display salary grades based on salary amounts:
15. CREATE TABLE
Create a new table for departments:
16. ALTER TABLE
Add a new column to store phone numbers:
Drop a column:
17. JOIN Multiple Tables
Combine employee and department data along with projects:
18. STORED PROCEDURE
Create a procedure to retrieve employee details by department:
Execute the procedure:
19. TRANSACTIONS
Perform operations with rollback capability:
These examples cover a variety of scenarios to help you master SQL queries and their practical applications.