Beginner’s Guide to SQL with Practical
Examples
Introduction
SQL (Structured Query Language) is the standard language for interacting with relational
databases. Whether you're a developer, analyst, or beginner, SQL is an essential skill for
working with data.
1. What is SQL?
SQL is a language used to manage and manipulate data in a relational database. It allows
users to query, insert, update, and delete data.
2. Basic SQL Commands
Some of the most commonly used SQL commands include:
- SELECT: Retrieve data from a table
- INSERT: Add new data
- UPDATE: Modify existing data
- DELETE: Remove data
3. SELECT Statement
The SELECT statement is used to retrieve data.
Example:
SELECT * FROM Employees;
4. WHERE Clause
The WHERE clause filters records.
Example:
SELECT * FROM Employees WHERE Department = 'HR';
5. ORDER BY Clause
ORDER BY is used to sort the results.
Example:
SELECT * FROM Employees ORDER BY Salary DESC;
6. GROUP BY Clause
GROUP BY groups rows that have the same values.
Example:
SELECT Department, COUNT(*) FROM Employees GROUP BY Department;
7. JOINs in SQL
Joins are used to combine rows from two or more tables.
Types:
- INNER JOIN
- LEFT JOIN
- RIGHT JOIN
- FULL OUTER JOIN
8. SQL Functions
Common SQL functions include:
- COUNT()
- SUM()
- AVG()
- MAX()
- MIN()
9. Subqueries
Subqueries are queries within queries.
Example:
SELECT Name FROM Employees WHERE Salary > (SELECT AVG(Salary) FROM Employees);
10. Practice Questions
Try writing queries for these tasks:
1. Retrieve names of employees in the 'IT' department.
2. Find the average salary in each department.
3. List employees who earn more than the average salary.
Conclusion
This guide has introduced you to basic SQL concepts and syntax. Practice these examples
and you'll gain confidence in querying databases.