SQL Tutorial
Overview
SQL (Structured Query Language) is a standard language for managing and manipulating relational databases. This document provides an introduction to SQL commands with examples.
Basic SQL Commands
1. SELECT
The SELECT
statement retrieves data from a database.
Example
2. WHERE
The WHERE
clause filters records based on specific conditions.
Example
3. INSERT INTO
The INSERT INTO
statement adds new rows to a table.
Example
4. UPDATE
The UPDATE
statement modifies existing records.
Example
5. DELETE
The DELETE
statement removes rows from a table.
Example
6. CREATE TABLE
The CREATE TABLE
statement creates a new table.
Example
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50)
);
7. ALTER TABLE
The ALTER TABLE
statement modifies an existing table structure.
Example
8. DROP TABLE
The DROP TABLE
statement deletes an entire table.
Example
9. JOIN
The JOIN
clause combines rows from two or more tables based on a related column.
INNER JOIN
Example
SELECT employees.first_name, departments.department_name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.department_id;
LEFT JOIN
Example
SELECT employees.first_name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;
Advanced SQL Commands
1. GROUP BY
The GROUP BY
statement groups rows sharing a property.
Example
2. HAVING
The HAVING
clause filters groups based on conditions.
Example
3. ORDER BY
The ORDER BY
clause sorts the result set.
Example
4. DISTINCT
The DISTINCT
keyword removes duplicate values.
Example
5. Subqueries
A subquery is a query within another query.
Example
SELECT first_name
FROM employees
WHERE department_id = (SELECT department_id FROM departments WHERE department_name = 'Marketing');
Summary
This tutorial introduces fundamental SQL commands used to interact with relational databases. Understanding these commands will enable you to effectively query and manipulate data. For more advanced features and detailed information, refer to the official SQL documentation.