INTRODUCTION TO SQL
Agenda – Session 1: Introduction to SQL
SELECT
INSERT
UPDATE
DELETE
WHERE Clause
ORDER BY
LIMIT
Aliasing (AS)
GROUP BY
HAVING vs WHERE
WHAT IS SQL
SQL stands for Structured Query Language.
It is used to communicate with databases. SQL is used to create, read, update, and delete data (commonly called CRUD operations).
SQL works with Relational Databases like MySQL, PostgreSQL, SQL Server, Oracle, etc.
WHY SQL IS IMPORTANT
Data is everywhere – apps, websites, banks, hospitals, social media – all run on databases.
SQL helps you interact with that data in a structured and powerful way.
It’s not just for developers — data analysts, testers, business analysts, and even product managers use SQL every day.
SQL is a top in-demand skill — often tested in interviews for internships and jobs.
1. DDL – Data Definition Language
Used to define or modify the structure of database objects (tables, schemas, etc.).
CREATE – Create a new table or database
ALTER – Modify the structure of an existing table
DROP – Delete a table or database
TYPES OF SQL COMMANDS
2. DML – Data Manipulation Language
Used to manage data within tables (insert, update, delete records).
INSERT – Add new records
UPDATE – Modify existing records
DELETE – Remove records
3. DCL – Data Control Language
Used to control access and permissions in the database.
GRANT – Provide access rights
REVOKE – Remove access rights
4. TCL – Transaction Control Language
Used to manage transactions in the database.
COMMIT – Save changes permanently
ROLLBACK – Undo changes
SAVEPOINT – Mark a point in a transaction for partial rollback
Creating and Populating a Table in SQL
1. Creating a Database
CREATE DATABASE college;
USE college;
2. Creating a Table
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
grade CHAR(2)
);
3. Inserting Data into the Table
INSERT INTO students (id, name, age, grade)
VALUES (1, 'Ananya', 20, 'A');
INSERT INTO students (id, name, age, grade)
VALUES (2, 'Rahul', 21, 'B’);
Explanation:
INSERT INTO specifies the table and columns.
VALUES provides the actual data in the same order as the columns.
INSERT INTO students (id, name, age, grade)
VALUES
(3, 'Sneha', 19, 'A'),
(4, 'Vikram', 22, 'C');
CORE SQL COMMANDS
COMMAND | SYNTAX | EXAMPLE |
SELECT – Retrieve Data | SELECT column1, column2, ... FROM table_name WHERE condition; | SELECT name, grade FROM students WHERE grade = 'A'; |
INSERT – Add New Data | INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...); | INSERT INTO students (id, name, age, grade) VALUES (6, 'Aakash', 20, 'B'); |
UPDATE – Modify Existing Data | UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition; | UPDATE students SET grade = 'A’ WHERE id = 6; |
DELETE – Remove Data | DELETE FROM table_name WHERE condition; | DELETE FROM students WHERE id = 6; |
Important Tip: Always use a WHERE clause with UPDATE and DELETE unless you intend to change/delete all records.
What Happens If You Don’t Use WHERE?
If you omit the WHERE clause in UPDATE or DELETE, the command will affect all rows in the table. This is one of the most common and dangerous mistakes in SQL.
Filtering Data with WHERE Clause
The WHERE clause is used to filter records based on a specific condition.
Syntax:
SELECT column1, column2
FROM table_name
WHERE condition;
Example:
SELECT name, grade
FROM students
WHERE grade = 'A';
Retrieves only students who scored an 'A'.
Common Operators Used in WHERE:
= (equal)
!= or <> (not equal)
> , < , >=, <=
BETWEEN, IN, LIKE, IS NULL
Refining Query Results with ORDER BY and LIMIT
COMMAND | DEFINITION | SYNTAX | EXAMPLE |
ORDER BY – Sort Query Results | The ORDER BY clause is used to sort the result set by one or more columns in ascending (ASC) or descending (DESC) order. | SELECT column1, column2 FROM table_name ORDER BY column1 [ASC | DESC]; | SELECT name, grade FROM students ORDER BY grade DESC; Displays students sorted by grade from highest to lowest. |
LIMIT – Restrict the Number of Rows Returned | The LIMIT clause is used to control how many rows are returned from the result set. | SELECT column1, column2 FROM table_name LIMIT number; | SELECT * FROM students LIMIT 3; Returns the first 3 records from the students table. |
Common Use Together:
SELECT name, grade FROM students ORDER BY grade DESC LIMIT 2;
Returns the top 2 students with the highest grades
Simplifying Output with SQL Aliases
What is Aliasing in SQL?
Aliasing is used to rename columns or tables temporarily to make results more readable or concise, especially in complex queries.
Column Alias Syntax:
SELECT column_name AS alias_name
FROM table_name;
Example:
SELECT name AS student_name, grade AS final_grade
FROM students;
Renames name to student_name and grade to final_grade in the result.
Table Aliasing in SQL
Query:
SELECT t.name
FROM students AS t;
students is the original table.
t is an alias (temporary name) given to students.
We then use t to refer to the table instead of writing students again.
Adding Two Columns
Suppose you have a table called salary_info:
emp_id base_salary bonus
101 40000 5000
Query:
SELECT emp_id, base_salary + bonus AS total_salary
FROM salary_info;
This adds base_salary and bonus and returns it as total_salary.
Example with JOIN:
SELECT s.name, d.department_name
FROM students AS s
JOIN departments AS d ON s.dept_id = d.dept_id;
Here, s and d are table aliases for readability.
Using GROUP BY and HAVING Clauses in SQL
GROUP BY – Group Rows with Similar Values
GROUP BY is used with aggregate functions like COUNT(), SUM(), AVG(), etc., to group rows based on one or more columns.
Syntax:
SELECT column, AGGREGATE_FUNCTION(column)
FROM table_name
GROUP BY column;
Example:
SELECT grade, COUNT(*) AS count_of_students
FROM students
GROUP BY grade;
Groups students by grade and counts how many are in each grade.
HAVING – Filter Groups After Aggregation
Unlike WHERE, which filters before grouping, HAVING filters after the group results are generated.
Syntax:
SELECT column, AGGREGATE_FUNCTION(column)
FROM table_name
GROUP BY column
HAVING condition;
Example:
SELECT grade, COUNT(*) AS count_of_students
FROM students
GROUP BY grade
HAVING COUNT(*) > 1;
Displays only the grades that appear more than once.
Clause | Filters... | Used With... |
WHERE | Individual rows | All queries |
HAVING | Grouped records | Aggregated queries |
WHERE vs HAVING
THANK YOU