0% found this document useful (0 votes)
5 views7 pages

SQL Topics Complete Guide

The document provides a comprehensive overview of SQL, covering essential topics such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Query Language (DQL), and more. Each section includes key commands, examples, and explanations of concepts like joins, subqueries, views, and normalization. It serves as a resource for understanding SQL syntax and functionality for database management.

Uploaded by

Aditya Katkhede
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

SQL Topics Complete Guide

The document provides a comprehensive overview of SQL, covering essential topics such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Query Language (DQL), and more. Each section includes key commands, examples, and explanations of concepts like joins, subqueries, views, and normalization. It serves as a resource for understanding SQL syntax and functionality for database management.

Uploaded by

Aditya Katkhede
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SQL Complete Topics with Examples and Queries

1. Introduction to SQL

SQL (Structured Query Language) is used to communicate with databases. It is the standard language
for relational database management systems.

2. Data Definition Language (DDL)

Used to define and modify database structures.

Commands:

• CREATE: Creates a new table.


• ALTER: Modifies existing table.
• DROP: Deletes table.
• TRUNCATE: Removes all records from table.

Example:

CREATE TABLE Students (


ID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT
);

ALTER TABLE Students ADD Email VARCHAR(100);

DROP TABLE Students;

TRUNCATE TABLE Students;

3. Data Manipulation Language (DML)

Used for inserting, updating, and deleting data.

Commands:

• INSERT: Adds data.


• UPDATE: Modifies data.
• DELETE: Removes data.

Example:

1
INSERT INTO Students (ID, Name, Age) VALUES (1, 'John', 20);

UPDATE Students SET Age = 21 WHERE ID = 1;

DELETE FROM Students WHERE ID = 1;

4. Data Query Language (DQL)

Used to query the database.

Command:

• SELECT

Example:

SELECT * FROM Students;


SELECT Name, Age FROM Students WHERE Age > 18;

5. Data Control Language (DCL)

Used to control access.

Commands:

• GRANT: Gives user access.


• REVOKE: Removes access.

Example:

GRANT SELECT ON Students TO user1;


REVOKE SELECT ON Students FROM user1;

6. Transaction Control Language (TCL)

Manages transactions.

Commands:

• COMMIT: Saves changes.


• ROLLBACK: Undoes changes.
• SAVEPOINT: Sets a savepoint.

2
Example:

BEGIN;
UPDATE Students SET Age = 22 WHERE ID = 1;
SAVEPOINT mySavepoint;
ROLLBACK TO mySavepoint;
COMMIT;

7. Clauses

• WHERE: Filters rows.


• ORDER BY: Sorts data.
• GROUP BY: Groups rows.
• HAVING: Filters groups.

Example:

SELECT Name FROM Students WHERE Age > 18 ORDER BY Age;


SELECT Age, COUNT(*) FROM Students GROUP BY Age HAVING COUNT(*) > 1;

8. Joins

Used to combine rows from two or more tables.

Types:

• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• FULL JOIN

Example:

SELECT s.Name, c.CourseName


FROM Students s
INNER JOIN Courses c ON s.ID = c.StudentID;

9. Subqueries

A query within another query.

Example:

3
SELECT Name FROM Students WHERE Age = (SELECT MAX(Age) FROM Students);

10. Views

A virtual table based on SQL result.

Example:

CREATE VIEW StudentView AS


SELECT Name, Age FROM Students WHERE Age > 18;

11. Indexes

Used to speed up searches.

Example:

CREATE INDEX idx_name ON Students(Name);

12. Constraints

Used to enforce data rules.

• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
• CHECK

Example:

CREATE TABLE Students (


ID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Age INT CHECK (Age >= 18)
);

4
13. Normalization

Organizing data to reduce redundancy.

• 1NF, 2NF, 3NF are common forms.

14. Stored Procedures

Stored SQL code.

Example:

CREATE PROCEDURE GetStudents()


BEGIN
SELECT * FROM Students;
END;

15. Functions

Returns a single value.

Example:

CREATE FUNCTION GetTotalStudents()


RETURNS INT
BEGIN
DECLARE total INT;
SELECT COUNT(*) INTO total FROM Students;
RETURN total;
END;

16. Triggers

SQL code that runs automatically on events.

Example:

CREATE TRIGGER before_insert_students


BEFORE INSERT ON Students
FOR EACH ROW
BEGIN

5
SET NEW.Age = IF(NEW.Age < 18, 18, NEW.Age);
END;

17. UNION and UNION ALL

Combines results of two queries.

Example:

SELECT Name FROM Students


UNION
SELECT Name FROM Teachers;

18. CASE Statement

Conditional logic in SQL.

Example:

SELECT Name,
CASE
WHEN Age < 18 THEN 'Minor'
ELSE 'Adult'
END AS AgeGroup
FROM Students;

19. Common Table Expressions (CTE)

Temporary result set used in a query.

Example:

WITH YoungStudents AS (
SELECT * FROM Students WHERE Age < 25
)
SELECT * FROM YoungStudents;

20. Window Functions

Perform calculations across rows.

6
Example:

SELECT Name, Age,


RANK() OVER (ORDER BY Age DESC) AS AgeRank
FROM Students;

This sheet includes almost all essential SQL topics with relevant examples and syntax. Let me know if
you want this in downloadable format (PDF/Excel/Word).

You might also like