Introduction to Databases
Introduction to Databases
Explanation:
COUNT(*): Counts the number of rows in each group.
GROUP BY column_name(ex:section): Groups the records based on the
specified column.
The ORDER BY clause in SQL is used to sort the result set based on one or more
columns in ascending or descending order.
The SUM() function calculates the total sum of a numeric column. The GROUP
BY clause groups the records, and the HAVING clause filters the grouped results
based on conditions.
Explanation:
SUM(fees): Calculates the total fees for each class.
GROUP BY class: Groups the records by class.
HAVING SUM(salary) <= 15000: Filters the results to show only class
where the total fees exceeds 15,000.
The HAVING clause is used instead of WHERE because aggregate functions
(SUM, AVG, etc.) cannot be used directly in WHERE.
Creating a new table named Sales where we do the group functions
Explanation of Group Functions:
COUNT(): This function counts the number of rows in a column, typically to find
the number of occurrences of a certain value.
COUNT(OrderID) counts how many orders were placed for each product.
AVG(): The AVG() function calculates the average of a numeric column.
MIN(Quantity) finds the least quantity ordered in a single order for each
product.
MAX(): The MAX() function identifies the maximum value in a numeric column.
MAX(Quantity) gives the highest quantity ordered for each product.
For example, a price of 5.00 remains 5.00, but if there were a price like 5.678,
it would be rounded to 5.68.
1. INNER JOIN
The INNER JOIN returns only the rows that have matching values in both tables.
Explanation:
The INNER JOIN will return only the customers who have placed orders, i.e.,
where the CustomerID is found in both the Customers and Orders tables.
Explanation:
The RIGHT JOIN will return all orders, even if they do not have a corresponding
customer (though in this case, every order has a corresponding customer).
Conclusion
INNER JOIN: Returns only matching rows from both tables.
LEFT JOIN: Returns all rows from the left table and matching rows from the
right table. Non-matching rows from the right table will have NULL values.
RIGHT JOIN: Returns all rows from the right table and matching rows from the
left table. Non-matching rows from the left table will have NULL values.
FULL JOIN: Returns all rows from both tables. Non-matching rows will have
NULL values in the missing table's columns.
These joins are essential for combining related data from multiple tables based
on common keys or conditions.
Subqueries in SQL
A subquery (also called an inner query or nested query) is a query that is
embedded inside another query. It is used to perform operations that require a
result from one query to be used in another query.
Example Tables
We’ll use the same Customers and Orders tables from the previous examples
for these queries.
Customers Table:
CustomerID CustomerName Country
1 John Doe USA
2 Jane Smith UK
3 Sam Brown Canada
4 Lisa White Australia
Orders Table:
OrderID CustomerID OrderDate TotalAmount
101 1 2025-01-10 250.00
102 2 2025-01-15 300.00
103 1 2025-02-05 450.00
104 3 2025-02-20 100.00
105 2 2025-03-05 200.00
106 4 2025-03-10 150.00
1. Subquery in the SELECT Clause
A scalar subquery can be used in the SELECT clause to return a single value that
is calculated for each row of the main query.
Query:
Let's say we want to display the CustomerName along with the total number of
orders each customer has placed.
SELECT
CustomerName,
(SELECT COUNT(*) FROM Orders WHERE Orders.CustomerID =
Customers.CustomerID) AS TotalOrders
FROM Customers;
Explanation:
The subquery (SELECT COUNT(*) FROM Orders WHERE Orders.CustomerID =
Customers.CustomerID) counts the total orders for each customer.
The subquery uses the CustomerID from the outer Customers table to find the
count for each customer.
Result:
CustomerName TotalOrders
John Doe 2
Jane Smith 2
Sam Brown 1
Lisa White 1
2. Subquery in the WHERE Clause
A subquery in the WHERE clause is used to filter the results based on a
condition that involves another query.
Query:
Let’s say we want to find all customers who have placed more than 1 order.
Query:
Let’s say we want to display the total order amount per customer along with
the customer name.
SELECT
Customers.CustomerName,
SubQuery.TotalAmount
FROM Customers
JOIN (SELECT CustomerID, SUM(TotalAmount) AS TotalAmount FROM Orders
GROUP BY CustomerID) AS SubQuery
ON Customers.CustomerID = SubQuery.CustomerID;
Explanation:
The subquery (SELECT CustomerID, SUM(TotalAmount) AS TotalAmount FROM
Orders GROUP BY CustomerID) calculates the total order amount per customer.
The outer query joins this subquery with the Customers table to display the
CustomerName and their total order amount.
Result:
CustomerName TotalAmount
John Doe 700.00
Jane Smith 500.00
Sam Brown 100.00
Lisa White 150.00
4. Subquery in the HAVING Clause
A subquery in the HAVING clause is used to filter results after the aggregation is
done. This is useful when you want to filter based on aggregate values.
Query:
Let’s say we want to find customers who have placed orders with a total order
amount greater than 400.
SELECT
CustomerID,
SUM(TotalAmount) AS TotalOrderAmount
FROM Orders
GROUP BY CustomerID
HAVING SUM(TotalAmount) > (SELECT AVG(TotalAmount) FROM Orders);
Explanation:
The subquery (SELECT AVG(TotalAmount) FROM Orders) calculates the average
order amount across all customers.
The outer query groups orders by CustomerID and filters the customers whose
total order amount is greater than the calculated average.
Result:
CustomerID TotalOrderAmount
1 700.00
2 500.00
5. Correlated Subqueries
A correlated subquery is a subquery that references a column from the outer
query. This type of subquery depends on the outer query for its value.
Query:
Let's say we want to find customers who have placed orders greater than the
average amount of their own orders.
SELECT CustomerName, TotalAmount
FROM Customers c
WHERE EXISTS (
SELECT 1
FROM Orders o
WHERE o.CustomerID = c.CustomerID
AND o.TotalAmount > (SELECT AVG(TotalAmount) FROM Orders WHERE
CustomerID = c.CustomerID)
);
Explanation:
The subquery SELECT AVG(TotalAmount) FROM Orders WHERE CustomerID =
c.CustomerID calculates the average order amount for each customer.
The correlated subquery checks if the order amount for a particular customer is
greater than their own average order amount.
Result:
CustomerName TotalAmount
John Doe 450.00
Jane Smith 300.00
Summary of Subqueries
Subquery in SELECT clause: Used to calculate or fetch values for each row.
Subquery in WHERE clause: Used to filter results based on a condition from
another query.
Subquery in FROM clause: Used to treat the result of a subquery as a
temporary table (derived table).
Subquery in HAVING clause: Used to filter aggregated data based on a result
from another query.
Correlated Subqueries: A subquery that depends on values from the outer
query.
Subqueries are a powerful tool for querying and manipulating data in SQL. They
allow you to perform complex operations that would otherwise require
multiple queries or additional steps.
Data manipulation in DBMS refers to the process of interacting with the data
stored in a database to retrieve, modify, or remove it. These operations are
generally performed using a language like SQL (Structured Query Language).
The main operations in data manipulation are retrieving data (SELECT),
inserting data (INSERT), updating data (UPDATE), and deleting data (DELETE).
The SELECT statement is used to retrieve data from one or more tables in a
database.
Example:
SELECT * FROM Employees;
This query retrieves all columns and rows from the "Employees" table.
INSERT (Adding Data):
Example:
SELECT * FROM Employees
WHERE Age > 30;
ORDER BY: To sort the results of a query.
Example:
SELECT * FROM Employees
ORDER BY Age DESC;
GROUP BY: To group data and perform aggregate functions like SUM, AVG,
COUNT.
Example:
SELECT Department, COUNT(*) FROM Employees
GROUP BY Department;
Example Scenario:
Let's assume you have a database with the following table structure:
Table: Employees
UPDATE Employees
SET Age = Age + 1
WHERE Department = 'Finance';
DELETE Example: If you want to delete the record of the employee with ID 101:
Data Definition Language (DDL) commands are used to define and manage the
structure of a database, including creating, altering, or deleting tables and
other database objects. DDL commands do not deal with data manipulation
directly (like retrieving or modifying data), but rather focus on defining the
schema or structure.
1. CREATE Table:
The CREATE command is used to create a new table in the database with
specific columns and their data types.
Example:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT,
Department VARCHAR(50)
);
This command creates a new table called "Employees" with four columns:
EmployeeID, Name, Age, and Department.
2. ALTER Table:
The ALTER command is used to modify an existing table's structure, such as
adding or dropping columns, changing column types, or renaming the table.
Example 1: Add a column
ALTER TABLE Employees
ADD Salary DECIMAL(10, 2);
This command adds a new column Salary to the "Employees" table.
3. DROP Table:
The DROP command is used to remove a table from the database entirely,
including all of its data, structure, and indexes. Be careful when using this
command because it is irreversible.
Example:
DROP TABLE Employees;
This command deletes the "Employees" table from the database.
4. TRUNCATE Table:
The TRUNCATE command is used to remove all records from a table but does
not affect the table structure. It is faster than DELETE because it does not log
each row deletion individually and does not fire triggers. However, TRUNCATE
is also irreversible.
Example:
TRUNCATE TABLE Employees;
This command deletes all records from the "Employees" table, but the table
structure (columns, data types) remains unchanged.
Example:
RENAME TABLE Employees TO Staff;
This renames the "Employees" table to "Staff".
6. COMMENT:
The COMMENT command is used to add a description or a comment to a table
or a column.
Example:
COMMENT ON COLUMN Employees.Age IS 'Age of the employee in years';
This adds a comment to the Age column of the "Employees" table, which can
help users or developers understand the purpose of that column.
Example Scenario:
Let’s assume you are working with the following table Employees:
Conclusion:
DDL commands in DBMS are essential for defining and modifying the structure
of database objects like tables. Commands like CREATE, ALTER, DROP, and
TRUNCATE allow you to manage database schemas and efficiently handle
database objects. These commands are mostly used by database
administrators and developers for schema changes or maintenance tasks.