Introduction to SQL
What is SQL?
• - SQL is a standard programming language
used to manage and manipulate databases.
• - It is used for querying, updating, and
managing data.
Types of SQL Commands
• - Data Query Language (DQL): SELECT
• - Data Definition Language (DDL): CREATE,
ALTER, DROP
• - Data Manipulation Language (DML): INSERT,
UPDATE, DELETE
• - Data Control Language (DCL): GRANT,
REVOKE
Why Learn SQL?
• - SQL is essential for data manipulation and
retrieval in databases.
• - It's widely used in data analysis, data science,
and web development.
Basic SQL Syntax
• - SQL statements follow a specific syntax:
• SELECT <columns> FROM <table> WHERE
<condition>;
SQL SELECT and WHERE Clauses
SELECT Clause
• - The SELECT statement is used to select data
from a database.
• - Example: SELECT column1, column2 FROM
table_name;
WHERE Clause
• - The WHERE clause filters records based on
specified conditions.
• - Example: SELECT column1 FROM table_name
WHERE condition;
Operators in WHERE Clause
• - Comparison Operators: =, >, <, >=, <=, <>
• - Logical Operators: AND, OR, NOT
Using Wildcards
• - Wildcards in SQL: % (represents zero or more
characters), _ (represents a single character)
• - Example: SELECT column1 FROM table_name
WHERE column2 LIKE 'A%';
SQL Joins
What is a JOIN?
• - JOIN combines rows from two or more tables
based on a related column.
• - Commonly used in relational databases to
retrieve data from multiple tables.
Types of Joins
• - INNER JOIN: Returns records that have
matching values in both tables.
• - LEFT JOIN (OUTER): Returns all records from
the left table and matched records from the
right table.
• - RIGHT JOIN (OUTER): Returns all records
from the right table and matched records from
the left table.
• - FULL JOIN (OUTER): Returns all records when
Example of INNER JOIN
• Example:
• SELECT [Link], b.order_date FROM
customers a
• INNER JOIN orders b ON a.customer_id =
b.customer_id;
Join in Action
• (Visual diagram showing two tables being
joined)
SQL Aggregate Functions
What are Aggregate Functions?
• - Aggregate functions perform calculations on
a set of values and return a single result.
Common Aggregate Functions
• - COUNT(): Counts the number of rows.
• - SUM(): Returns the total sum.
• - AVG(): Returns the average value.
• - MIN(): Returns the minimum value.
• - MAX(): Returns the maximum value.
Example of Aggregate Functions
• - Example: SELECT COUNT(*) FROM
table_name;
• - Example: SELECT AVG(price) FROM products;
GROUP BY and HAVING Clauses
• - GROUP BY: Groups rows sharing a property
to aggregate data.
• - HAVING: Filters groups based on an
aggregate condition.
• Example:
• SELECT COUNT(*), country FROM customers
• GROUP BY country HAVING COUNT(*) > 10;
SQL Constraints and Keys
What are Constraints?
• - Constraints enforce rules on data in a table.
• - Common types: NOT NULL, UNIQUE,
PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT
What are Keys?
• - Primary Key: A unique identifier for each
record in a table.
• - Foreign Key: A field in a table that uniquely
identifies a row in another table.
Examples of Constraints
• - Example: CREATE TABLE students (id INT
PRIMARY KEY, name VARCHAR(50) NOT NULL);
• - Example: ALTER TABLE orders ADD
CONSTRAINT fk_customer FOREIGN KEY
(customer_id) REFERENCES customers(id);
Importance of Keys and Constraints
• - Ensure data integrity and consistency.
• - Primary and foreign keys establish
relationships between tables.