Module 2 Introduction to SQL
Module 2 Introduction to SQL
Introduction to SQL
Key Concepts
o SQL is used to interact with RDBMS, which stores data in tables. Examples of
RDBMS include MySQL, PostgreSQL, Oracle, SQL Server, and SQLite.
2. SQL Syntax:
o SQL syntax is the set of rules that defines the structure and format of SQL
statements.
The SELECT statement is one of the most fundamental and widely used commands in SQL. It is
used to retrieve data from one or more tables in a database. Here’s a detailed explanation of
basic SELECT queries with examples:
Basic Syntax
EX.
FROM table_name;
Examples
To retrieve all columns from a table, you can use the * wildcard:
EX.
This query retrieves all columns and rows from the Employees table.
2. Selecting Specific Columns
To retrieve specific columns, list the column names after the SELECT keyword:
EX.
This query retrieves only the FirstName and LastName columns from the Employees table.
EX.
This query retrieves the first and last names of employees who work in the Sales department.
The ORDER BY clause is used to sort the result set by one or more columns:
EX.
This query retrieves the first and last names of employees, sorted by last name in ascending
order.
Aggregate functions perform calculations on multiple rows and return a single value:
EX.
This query counts the total number of employees in the Employees table.
The GROUP BY clause groups rows that have the same values in specified columns into
summary rows:
EX.
EX.
SELECT Department, COUNT(*) AS NumberOfEmployees FROM Employees GROUP BY
Department HAVING COUNT(*) > 10;
8. Using Aliases
EX.
This query retrieves the first and last names of employees, with the columns renamed
to FName and LName.
EX.
The ORDER BY clause in SQL is used to sort the result set of a query by one or more columns. By
default, the ORDER BY clause sorts the data in ascending order. However, you can specify
descending order using the DESC keyword.
Syntax
FROM table_name
Ex.
ORDER BY LastName;
This query retrieves all columns from the Employees table and sorts the rows by
the LastName column in ascending order.
Ex.
SELECT * FROM Employees
This query retrieves all columns from the Employees table and sorts the rows by
the LastName column in descending order.
Ex.
This query retrieves all columns from the Employees table and sorts the rows first by
the Department column in ascending order, and then by the LastName column in ascending
order within each department.
To sort the result set by multiple columns with different sort orders:
Ex.
This query retrieves all columns from the Employees table and sorts the rows first by
the Department column in ascending order, and then by the LastName column in descending
order within each department.
Aggregate functions in SQL are used to perform calculations on multiple rows of a table’s
column and return a single value. These functions are often used with the GROUP BY clause to
group rows that share a property so that an aggregate function can be applied to each group.
Here are some commonly used aggregate functions, along with examples:
1. COUNT(): Counts the number of rows in a table or the number of non-NULL values in a
column.
Ex.
This query counts the total number of rows in the Employees table.
2. SUM(): Calculates the total sum of a numeric column.
Ex.
This query calculates the total sum of the Salary column in the Employees table.
EX.
EX.
EX.
The GROUP BY clause is used to group rows that have the same values in specified columns
into summary rows. Aggregate functions are then applied to each group.
Example:
FROM Employees
GROUP BY Department;
This query groups the employees by department and calculates the number of employees and
the average salary for each department.
The HAVING clause in SQL is used to filter groups of rows based on a specified condition,
similar to how the WHERE clause filters individual rows. The HAVING clause is typically used in
conjunction with the GROUP BY clause to apply conditions to groups of rows after they have
been aggregated.
Syntax
SELECT column1, aggregate_function(column2)
FROM table_name
GROUP BY column1
HAVING condition;
• GROUP BY: Groups the rows that have the same values in specified columns.
Example
Consider a table named Sales with the following columns: SalesID, ProductID, Quantity,
and SaleDate.
Table: Sales
1 101 5 2024-01-01
2 102 3 2024-01-02
3 101 7 2024-01-03
4 103 2 2024-01-04
5 102 6 2024-01-05
To find products that have been sold in quantities greater than 10, you can use
the HAVING clause with the GROUP BY clause:
EX.
FROM Sales
GROUP BY ProductID
Explanation:
• SELECT ProductID, SUM(Quantity) AS TotalQuantity: Selects the ProductID and the total
quantity sold for each product.
• HAVING SUM(Quantity) > 10: Filters the groups to include only those products where the
total quantity sold is greater than 10.
Result:
ProductID TotalQuantity
101 12
102 9
In this example, only ProductID 101 meets the condition specified in the HAVING clause.
Subqueries in SQL
A subquery, also known as a nested query or inner query, is a query embedded within another
SQL query. The outer query, which contains the subquery, is referred to as the main query or
outer query. Subqueries are used to perform operations that require data from multiple tables or
to perform calculations on a subset of data within a larger result set.
Syntax
FROM table_name
Ex.
FROM employees
Explanation: The subquery retrieves department_id values from the departments table
where location_id is 1700. The outer query then retrieves employees
whose department_id matches any of these values.
Ex.
FROM departments
Explanation: The subquery checks for the existence of employees in each department. The
outer query retrieves departments where the subquery returns at least one row.
Keys are fundamental components of relational databases. They are used to identify and
establish relationships between records in different tables, ensuring data integrity and enabling
efficient querying and data manipulation. Here’s a detailed explanation of the different types of
keys in SQL:
1. Primary Key
• Characteristics:
Ex.
FirstName VARCHAR(50),
LastName VARCHAR(50),
BirthDate DATE
);
In this example, EmployeeID is the primary key for the Employees table, ensuring each
employee has a unique identifier.
2. Foreign Key
• Definition: A foreign key is a column or a set of columns in one table that references the
primary key columns of another table.
• Characteristics:
o Ensures referential integrity between tables.
o The values in the foreign key column must match values in the referenced
primary key column or be NULL.
Ex.
OrderDate DATE,
CustomerID INT,
);
Here, CustomerID in the Orders table is a foreign key that references CustomerID in
the Customers table.
3. Composite Key
• Characteristics:
Ex.
StudentID INT,
CourseID INT,
EnrollmentDate DATE,
);
Here, the combination of StudentID and CourseID forms the composite primary key for
the Enrollments table.
4. Candidate Key
• Definition: A candidate key is a column or a set of columns that can uniquely identify a
row in a table. It is a minimal super key.
• Characteristics:
StudentID INT,
Email VARCHAR(100),
UNIQUE (Email)
);
In this example, both StudentID and Email are candidate keys, but StudentID is chosen as the
primary key.
5. Surrogate Key
• Definition: A surrogate key is an artificial key that is used as a substitute for a natural
key.
• Characteristics:
Ex.
ProductName VARCHAR(100)
);
Constraints in SQL
Constraints in SQL are rules applied to columns in a table to ensure the accuracy, reliability, and
integrity of the data. They limit the type of data that can be inserted into a table and enforce
rules at the database level. Here are the main types of constraints in SQL:
Types of Constraints
Ex.
BirthDate DATE
);
In this example, the EmployeeID, FirstName, and LastName columns cannot contain NULL
values.
2. UNIQUE Constraint
Ex.
);
Here, the Email column must contain unique values, ensuring no two employees can have the
same email address.
o Definition: A combination of NOT NULL and UNIQUE. Uniquely identifies each row
in a table.
Ex.
FirstName VARCHAR(50),
LastName VARCHAR(50)
);
In this example, EmployeeID is the primary key, uniquely identifying each employee.
o Definition: Ensures that values in a column (or a set of columns) match values in
another table’s column(s), maintaining referential integrity.
Ex.
OrderDate DATE,
CustomerID INT,
);
Here, CustomerID in the Orders table is a foreign key that references CustomerID in
the Customers table.
5. CHECK Constraint
Ex.
);
This example ensures that the Salary column only contains positive values.
6. DEFAULT Constraint
Ex.
FirstName VARCHAR(50),
LastName VARCHAR(50),
);
In this example, if no hire date is specified, the HireDate column will default to the current date.
Applying Constraints
Constraints can be applied at the time of table creation using the CREATE TABLE statement or
added to an existing table using the ALTER TABLE statement.
This statement adds a CHECK constraint to the Employees table to ensure that
the Salary column contains only positive values.
Data Definition Commands in SQL
Data Definition Language (DDL) commands are used to define and manage database
structures, such as tables, indexes, and schemas. These commands are essential for creating,
modifying, and deleting database objects. Here are the main DDL commands, along with their
syntax and examples:
1. CREATE
The CREATE command is used to create new database objects, such as tables, indexes, views,
and schemas.
Syntax:
column1 datatype,
column2 datatype,
...
);
Example:
FirstName VARCHAR(50),
LastName VARCHAR(50),
BirthDate DATE,
);
2. ALTER
The ALTER command is used to modify the structure of an existing database object, such as
adding, deleting, or modifying columns in a table.
Syntax:
Example:
3. DROP
The DROP command is used to delete an existing database object, such as a table, index, or
view.
Syntax:
Example:
4. TRUNCATE
The TRUNCATE command is used to remove all rows from a table, but the table structure
remains intact.
Syntax:
Example:
This example removes all rows from the Employees table, but the table structure is preserved.
5. RENAME
The RENAME command is used to change the name of an existing database object.
Syntax:
Example:
Views in SQL
A view in SQL is a virtual table that is based on the result set of an SQL query. Unlike a physical
table, a view does not store data itself but provides a way to look at data from one or more
tables. Views are used to simplify complex queries, enhance security by restricting access to
specific data, and present data in a specific format without altering the underlying tables.
Key Concepts
1. Virtual Table: A view behaves like a table but does not store data physically. It is a
stored query that can be treated as a table.
3. Security: Views can restrict access to specific columns or rows, providing a layer of
security.
Creating a View
Syntax:
FROM table_name
WHERE condition;
• AS SELECT column1, column2, ...: Defines the columns to include in the view.
• FROM table_name: Specifies the table from which to retrieve the data.
Example:
FROM Employees;
This view, named EmployeeNames, includes the FirstName and LastName columns from
the Employees table.
Querying a View
Once a view is created, you can query it just like a regular table.
Example:
This query retrieves all rows and columns from the EmployeeNames view.
Modifying a View
To modify an existing view, you can use the CREATE OR REPLACE VIEW statement.
Example:
FROM Employees;
This statement modifies the EmployeeNames view to include the Email column.
Dropping a View
Syntax:
Example:
1. Simplify Complex Queries: Views can encapsulate complex queries, making it easier
to reuse and maintain them.
2. Enhance Security: Views can restrict access to specific columns or rows, providing a
layer of security.
3. Data Abstraction: Views can present data in a specific format, hiding the complexity of
the underlying tables.
4. Consistency: Views can provide a consistent interface to data, even if the underlying
tables change.
Types of Views
1. Simple View: Based on a single table and does not contain functions or groups of data.
2. Complex View: Based on multiple tables and can contain functions, joins, and groups
of data.
FROM Employees e
A trigger in SQL is a special type of stored procedure that automatically executes in response to
certain events on a particular table or view. Triggers are used to enforce business rules,
maintain data integrity, and automate system tasks.
Key Concepts
2. Automatic Execution: Once defined, triggers execute automatically when the specified
event occurs.
3. Associated with Tables: Triggers are always associated with a particular table or view.
Types of Triggers
3. INSTEAD OF Triggers: Execute in place of the triggering event (commonly used with
views).
Syntax
ON table_name
BEGIN
-- trigger logic
END;
• BEFORE | AFTER | INSTEAD OF: Specifies when the trigger should execute.
• INSERT | UPDATE | DELETE: Specifies the event that activates the trigger.
• FOR EACH ROW: Specifies that the trigger should execute once for each row affected by
the event.
• BEGIN ... END: The block where the trigger logic is defined.
Examples
DELIMITER $$
BEGIN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'ERROR: Age must be at least 25 years!';
END IF;
END $$
DELIMITER ;
• Explanation: This trigger checks the Age of the new row being inserted. If the age is less
than 25, it raises an error.
A trigger that updates the total sales in the Products table after a new sale is recorded in
the Sales table:
DELIMITER $$
BEGIN
UPDATE Products
END $$
DELIMITER ;
• Explanation: This trigger updates the TotalSales column in the Products table whenever
a new sale is recorded.
$$
BEGIN
END $$
DELIMITER ;
• Explanation: This trigger inserts the old values of the row being deleted into
the EmployeesBackup table before the row is deleted from the Employees table.
2. Audit and Logging: Triggers can be used to log changes and maintain an audit trail.
3. Complex Validation: Triggers can perform complex validations that are not possible
with standard constraints.
3. Hidden Logic: Triggers can make the database logic less transparent, as the logic is not
visible in the application code.
Joins in MySQL
Joins in MySQL are used to combine rows from two or more tables based on a related column
between them. Joins are essential for querying data from multiple tables and establishing
relationships between different sets of information. Here are the main types of joins in MySQL,
along with examples:
Types of Joins
1. INNER JOIN
4. CROSS JOIN
1. INNER JOIN
The INNER JOIN keyword selects records that have matching values in both tables.
Syntax:
SELECT columns
FROM table1
ON table1.column = table2.column;
Example:
FROM Orders
Explanation: This query retrieves the OrderID, CustomerName, and OrderDate for orders where
there is a matching CustomerID in both the Orders and Customers tables.
The LEFT JOIN keyword returns all records from the left table (table1), and the matched records
from the right table (table2). The result is NULL from the right side if there is no match.
Syntax:
SELECT columns
FROM table1
ON table1.column = table2.column;
Example:
FROM Customers
Explanation: This query retrieves all customers and their orders. If a customer has no orders,
the OrderID will be NULL.
The RIGHT JOIN keyword returns all records from the right table (table2), and the matched
records from the left table (table1). The result is NULL from the left side if there is no match.
Syntax:
SELECT columns
FROM table1
ON table1.column = table2.column;
Example:
FROM Orders
Explanation: This query retrieves all employees and their orders. If an employee has no orders,
the OrderID will be NULL.
4. CROSS JOIN
The CROSS JOIN keyword returns the Cartesian product of the two tables, meaning it returns all
possible combinations of rows from the two tables.
Syntax:
SELECT columns
FROM table1
FROM Products
Explanation: This query retrieves all possible combinations of products and categories.
Joins can be combined with other SQL clauses such as WHERE, GROUP BY, and HAVING to
filter and group the result set.
FROM Orders
Explanation: This query retrieves orders placed after January 1, 2024, along with the customer
names.
FROM Customers
GROUP BY Customers.CustomerName
Explanation: This query retrieves customers who have placed more than five orders, along with
the count of their orders.