1. Introduction to SQL
● SQL (Structured Query Language) is the standard language used for managing
and querying relational databases. It helps to store, modify, and retrieve data from
databases.
● Relational Databases store data in tables consisting of rows and columns. Tables
can be linked using keys, such as Primary Key (unique identifier for rows) and
Foreign Key (establishes a relationship between two tables).
● Popular SQL Databases:
○ MySQL: Open-source, widely used for web applications.
○ PostgreSQL: Open-source, supports advanced features and is known for
data integrity.
○ SQL Server: Microsoft product, used for enterprise-level applications.
○ SQLite: Lightweight, embedded SQL database, often used in mobile apps.
2. SQL Basics
● Basic SQL Syntax: SQL commands typically consist of clauses like SELECT, FROM,
WHERE, ORDER BY, LIMIT, and more. Each clause has a specific purpose, and
understanding the structure of SQL queries is critical. \\
Examples:
SELECT Statement:
SELECT column1, column2 FROM table_name;
● Retrieves data from column1 and column2 from the table_name.
WHERE Clause: Filters rows based on a condition.
SELECT * FROM employees WHERE salary > 50000;
● Retrieves all employees with a salary greater than 50,000.
ORDER BY Clause: Sorts the result set.
SELECT * FROM employees ORDER BY salary DESC;
● Orders employees by salary in descending order.
LIMIT Clause: Limits the number of rows returned.
SELECT * FROM employees LIMIT 5;
● Returns only the first 5 rows from the employees table.
, 3. Data Retrieval with SQL (SELECT Statement)
● SELECT: Retrieves data from one or more columns in a table.
DISTINCT: Avoids duplicates in the result set.
SELECT DISTINCT department FROM employees;
WHERE Clause: Filters results based on a condition.
SELECT name, department FROM employees WHERE department = 'IT';
● AND, OR, NOT:
○ AND: Both conditions must be true.
SELECT * FROM employees WHERE salary > 50000 AND department = 'IT';
○ OR: Either condition can be true.
SELECT * FROM employees WHERE salary > 50000 OR department = 'HR';
○ NOT: Negates a condition.
SELECT * FROM employees WHERE NOT department = 'Finance';
● SQL (Structured Query Language) is the standard language used for managing
and querying relational databases. It helps to store, modify, and retrieve data from
databases.
● Relational Databases store data in tables consisting of rows and columns. Tables
can be linked using keys, such as Primary Key (unique identifier for rows) and
Foreign Key (establishes a relationship between two tables).
● Popular SQL Databases:
○ MySQL: Open-source, widely used for web applications.
○ PostgreSQL: Open-source, supports advanced features and is known for
data integrity.
○ SQL Server: Microsoft product, used for enterprise-level applications.
○ SQLite: Lightweight, embedded SQL database, often used in mobile apps.
2. SQL Basics
● Basic SQL Syntax: SQL commands typically consist of clauses like SELECT, FROM,
WHERE, ORDER BY, LIMIT, and more. Each clause has a specific purpose, and
understanding the structure of SQL queries is critical. \\
Examples:
SELECT Statement:
SELECT column1, column2 FROM table_name;
● Retrieves data from column1 and column2 from the table_name.
WHERE Clause: Filters rows based on a condition.
SELECT * FROM employees WHERE salary > 50000;
● Retrieves all employees with a salary greater than 50,000.
ORDER BY Clause: Sorts the result set.
SELECT * FROM employees ORDER BY salary DESC;
● Orders employees by salary in descending order.
LIMIT Clause: Limits the number of rows returned.
SELECT * FROM employees LIMIT 5;
● Returns only the first 5 rows from the employees table.
, 3. Data Retrieval with SQL (SELECT Statement)
● SELECT: Retrieves data from one or more columns in a table.
DISTINCT: Avoids duplicates in the result set.
SELECT DISTINCT department FROM employees;
WHERE Clause: Filters results based on a condition.
SELECT name, department FROM employees WHERE department = 'IT';
● AND, OR, NOT:
○ AND: Both conditions must be true.
SELECT * FROM employees WHERE salary > 50000 AND department = 'IT';
○ OR: Either condition can be true.
SELECT * FROM employees WHERE salary > 50000 OR department = 'HR';
○ NOT: Negates a condition.
SELECT * FROM employees WHERE NOT department = 'Finance';