SQL Lecture Notes Compilation
SQL Lecture Notes Compilation
Version)
Lecture 05: SQL Basics and Simple Queries
Topics Covered:
A Database Management System (DBMS) is software that helps manage databases by enabling
users to create, modify, and query data efficiently. It ensures data consistency, security, and
integrity while allowing multiple users to interact with the data concurrently.
Data Definition Language (DDL): Used to define and structure the database, including
creating and altering tables. Commands include CREATE, ALTER, and DROP.
Data Manipulation Language (DML): Used for data retrieval and modification.
Commands include SELECT, INSERT, UPDATE, and DELETE.
Data Control Language (DCL): Used to control access to the database. Commands
include GRANT and REVOKE.
SQL is the standard language for interacting with relational databases. It allows users to perform
the following operations, known as CRUD operations:
SELECT * FROM Employees WHERE salary > 50000 ORDER BY hire_date DESC;
This query retrieves all employees with a salary greater than 50,000 and sorts the results in
descending order by their hire date.
To store data in a relational database, you need to create tables. Each table consists of columns
(fields) and rows (records).
The CREATE TABLE statement is used to define a new table. You must specify the table name,
column names, and their data types.
Example:
Once the table is created, you can insert records using the INSERT INTO statement.
Example:
The SELECT statement is used to retrieve data from a table. You can specify which columns to
retrieve and apply conditions to filter the results.
Example:
This query retrieves the employee ID, current salary, and a new salary increased by 1,000.
7. Handling NULL Values
A NULL value represents missing or unknown data. It is important to handle NULL values
carefully in queries.
Example:
Example:
This query retrieves employees sorted by their hire date in descending order.
Joins allow you to retrieve data from multiple tables by combining rows based on a related
column. There are several types of joins:
Inner Join: Retrieves only the matching rows from both tables.
Left Join: Retrieves all rows from the left table and matching rows from the right table.
Right Join: Retrieves all rows from the right table and matching rows from the left table.
Full Join: Retrieves all rows from both tables.
Inner Join:
An inner join retrieves records that have matching values in both tables. Only rows that satisfy
the join condition are returned in the result set.
Syntax:
SELECT columns
FROM Table1
INNER JOIN Table2 ON Table1.common_column = Table2.common_column;
Example:
In this example, only employees who have a matching department in the Department table will
be included in the result set.
A left join retrieves all records from the left table and the matched records from the right table. If
there are no matches, the result will contain NULL values for the columns of the right table.
Syntax:
SELECT columns
FROM Table1
LEFT JOIN Table2 ON Table1.common_column = Table2.common_column;
Example:
This query retrieves all departments and their associated employees. If a department has no
employees, NULL values are returned for employee details.
A right join is the opposite of a left join. It retrieves all records from the right table and the
matched records from the left table.
Syntax:
SELECT columns
FROM Table1
RIGHT JOIN Table2 ON Table1.common_column = Table2.common_column;
Example:
A full join retrieves all records from both tables. If there are no matches, NULL values are
returned for the missing columns from each table.
Note: MySQL does not support the FULL OUTER JOIN clause directly. Instead, you can achieve
this by combining LEFT JOIN and RIGHT JOIN with the UNION operator.
Example:
In this example, all employees and departments are included in the result set, even if there are no
matches.
Self Join:
A self join is a regular join, but it is used to join a table to itself. This can be useful for
hierarchical data or finding relationships within the same table.
Syntax:
Example:
In this example, the query retrieves employees along with their respective managers.