0% found this document useful (0 votes)
8 views6 pages

SQL_Queries_with_Examples

The document provides an overview of SQL queries categorized into Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), and Transaction Control Language (TCL), along with examples for each. It also covers clauses in SELECT queries, various types of joins, constraints, and advanced SQL features such as subqueries, views, indexes, triggers, stored procedures, and functions. Each section includes practical examples to illustrate the usage of the SQL commands.

Uploaded by

Hadiya Mazhar
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)
8 views6 pages

SQL_Queries_with_Examples

The document provides an overview of SQL queries categorized into Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), and Transaction Control Language (TCL), along with examples for each. It also covers clauses in SELECT queries, various types of joins, constraints, and advanced SQL features such as subqueries, views, indexes, triggers, stored procedures, and functions. Each section includes practical examples to illustrate the usage of the SQL commands.

Uploaded by

Hadiya Mazhar
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
You are on page 1/ 6

SQL Queries and Their Uses

1. Data Definition Language (DDL)

CREATE TABLE - Creates a new table

Example:

CREATE TABLE Student (

ID INT PRIMARY KEY,

Name VARCHAR(50),

Age INT

);

ALTER TABLE - Modifies an existing table

Example:

ALTER TABLE Student ADD Email VARCHAR(100);

DROP TABLE - Deletes a table completely

Example:

DROP TABLE Student;

TRUNCATE TABLE - Deletes all data from a table

Example:

TRUNCATE TABLE Student;

2. Data Manipulation Language (DML)

SELECT - Retrieves data

Example:

SELECT * FROM Student;

INSERT INTO - Adds new data

Example:

INSERT INTO Student (ID, Name, Age) VALUES (1, 'Ali', 22);
SQL Queries and Their Uses

UPDATE - Modifies existing data

Example:

UPDATE Student SET Age = 23 WHERE ID = 1;

DELETE - Removes data

Example:

DELETE FROM Student WHERE ID = 1;

3. Data Control Language (DCL)

GRANT - Gives access

Example:

GRANT SELECT ON Student TO user123;

REVOKE - Removes access

Example:

REVOKE SELECT ON Student FROM user123;

4. Transaction Control Language (TCL)

COMMIT - Saves changes

Example:

COMMIT;

ROLLBACK - Undoes changes

Example:

ROLLBACK;

SAVEPOINT - Sets a checkpoint

Example:

SAVEPOINT sp1;
SQL Queries and Their Uses

SET TRANSACTION - Sets transaction properties

Example:

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

5. Clauses in SELECT Queries

WHERE - Filters results

Example:

SELECT * FROM Student WHERE Age > 20;

ORDER BY - Sorts results

Example:

SELECT * FROM Student ORDER BY Name;

GROUP BY - Groups records

Example:

SELECT Age, COUNT(*) FROM Student GROUP BY Age;

HAVING - Filters groups

Example:

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

DISTINCT - Removes duplicates

Example:

SELECT DISTINCT Name FROM Student;

LIMIT - Limits number of rows

Example (MySQL):

SELECT * FROM Student LIMIT 5;

6. Joins
SQL Queries and Their Uses

INNER JOIN - Matches in both tables

Example:

SELECT s.Name, r.Date FROM Student s

JOIN Reserves r ON s.ID = r.S_Id;

LEFT JOIN - All from left + matches from right

Example:

SELECT s.Name, r.Date FROM Student s

LEFT JOIN Reserves r ON s.ID = r.S_Id;

RIGHT JOIN - All from right + matches from left

Example:

SELECT s.Name, r.Date FROM Student s

RIGHT JOIN Reserves r ON s.ID = r.S_Id;

FULL OUTER JOIN - Combines both sides

Example (PostgreSQL):

SELECT s.Name, r.Date FROM Student s

FULL OUTER JOIN Reserves r ON s.ID = r.S_Id;

7. Constraints

PRIMARY KEY - Unique row identifier

Example:

ID INT PRIMARY KEY

FOREIGN KEY - Link tables

Example:

FOREIGN KEY (S_Id) REFERENCES Student(ID)

NOT NULL - No null allowed


SQL Queries and Their Uses

Example:

Name VARCHAR(50) NOT NULL

UNIQUE - No duplicates

Example:

Email VARCHAR(100) UNIQUE

CHECK - Condition-based validation

Example:

Age INT CHECK (Age >= 18)

DEFAULT - Auto value if none provided

Example:

Status VARCHAR(10) DEFAULT 'Active'

8. Advanced SQL Features

SUBQUERIES - Nested queries

Example:

SELECT * FROM Student WHERE Age > (SELECT AVG(Age) FROM Student);

VIEWS - Virtual tables

Example:

CREATE VIEW SeniorStudents AS SELECT * FROM Student WHERE Age >= 22;

INDEXES - Faster search

Example:

CREATE INDEX idx_name ON Student(Name);

TRIGGERS - Auto actions

Example:
SQL Queries and Their Uses

CREATE TRIGGER age_check BEFORE INSERT ON Student

FOR EACH ROW BEGIN

IF NEW.Age < 0 THEN

SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Age cannot be negative';

END IF;

END;

STORED PROCEDURES - Predefined tasks

Example:

CREATE PROCEDURE GetStudents()

BEGIN

SELECT * FROM Student;

END;

FUNCTIONS - Return values

Example:

CREATE FUNCTION getAgeCategory(age INT)

RETURNS VARCHAR(10)

RETURN CASE WHEN age >= 18 THEN 'Adult' ELSE 'Minor' END;

You might also like