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

SQL

SQL commands are categorized into five main types: DDL for defining database structures, DML for manipulating data, DQL for querying data, DCL for access control, and TCL for transaction management. Each type includes specific commands with examples, such as CREATE, INSERT, SELECT, GRANT, and COMMIT. Additionally, various SQL clauses and operators are used for filtering, sorting, and joining data in queries.

Uploaded by

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

SQL

SQL commands are categorized into five main types: DDL for defining database structures, DML for manipulating data, DQL for querying data, DCL for access control, and TCL for transaction management. Each type includes specific commands with examples, such as CREATE, INSERT, SELECT, GRANT, and COMMIT. Additionally, various SQL clauses and operators are used for filtering, sorting, and joining data in queries.

Uploaded by

ricota5910
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

SQL (Structured Query Language) commands are categorized into five main

types. Each type serves a specific purpose in managing and interacting with a
database. Here's a breakdown with short explanations and examples:

1. DDL – Data Definition Language


Used to define or modify the structure of database objects like tables,
schemas, etc.
Command Description Example
CREATE TABLE
CREATE Creates a new table or database Students (id INT,
name VARCHAR(50));
ALTER TABLE
Modifies an existing table --such as adding,
ALTER Students ADD age
deleting, or modifying columns.
INT;
Deletes a table or database .Once something
DROP TABLE
DROP is dropped, all the data and structure is lost
Students;
permanently.

2. DML – Data Manipulation Language


Used to manipulate data stored in tables.
Command Description Example
INSERT INTO Students (id,
INSERT Adds new records (rows)
name) VALUES (1, 'Alice');
Modifies existing data
UPDATE Students SET name =
UPDATE Always use a WHERE clause to avoid 'Bob' WHERE id = 1;
updating all rows unintentionally.

Removes records (rows) and can be DELETE FROM Students


DELETE
rollback WHERE id = 1;

3. DQL – Data Query Language


Used to query and retrieve data from the database.
Command Description Example
SELECT Fetches data from a table SELECT * FROM Students;

4. DCL – Data Control Language


Deals with permissions and access control.
Command Description Example
GRANT Gives user access rights GRANT SELECT ON Students TO user1;
REVOKE Removes access rights REVOKE SELECT ON Students FROM user1;

5. TCL – Transaction Control Language


Manages transactions to ensure data integrity.
Command Description Example
COMMIT Saves all changes made by the transaction COMMIT;
ROLLBACK Undoes changes since last commit ROLLBACK;
SAVEPOINT Sets a point to rollback to SAVEPOINT sp1;

SQL OPERATOR
✅ 1. WHERE
Used to filter rows based on a condition.
EX :-
SELECT * FROM Students
WHERE age > 18;
✅ 2. BETWEEN
Checks if a value lies within a range (inclusive).
SELECT * FROM Students
WHERE age BETWEEN 18 AND 25;

✅ 3. IN
Checks if a value matches any value in a list.

SELECT * FROM Students


WHERE grade IN ('A', 'B');

✅ 4. OR
Used to combine multiple conditions; returns true if any condition is true.

SELECT * FROM Students


WHERE age < 18 OR grade = 'A';

✅ 5. NOT NULL
Ensures a column cannot have NULL values when defining a table.

CREATE TABLE Students (


id INT,
name VARCHAR(100) NOT NULL -- name must be provided
);

✅ 6. IS NULL
Used to check if a column has NULL value.
SELECT * FROM Students
WHERE grade IS NULL;

✅ 7. IS NOT NULL
Used to check if a column does NOT have NULL value.

SELECT * FROM Students


WHERE grade IS NOT NULL;

🔹 Main SQL Clauses


These are building blocks used in most SQL queries.
Clause Purpose Example
SELECT name
SELECT Retrieves data
FROM Users;
SELECT * FROM
FROM Specifies table(s) to query from
Users;
SELECT * FROM
WHERE Filters rows based on condition Users WHERE age >
18;
SELECT age,
GROUP COUNT(*) FROM
Groups rows sharing a value
BY Users GROUP BY
age;

HAVING COUNT(*)
HAVING Filters groups (used with GROUP BY) HAVING
filters groups after grouping. HAVING is mostly > 1;
Clause Purpose Example

used with aggregate functions like SUM(), AVG(),


COUNT(), MAX(), MIN().

ORDER BY name
ORDER BY Sorts result set in ascending or descending order
ASC;
LIMIT 5; or SELECT
LIMIT /
Limits number of rows (MySQL/SQL Server) TOP 5 * FROM
TOP
Users;
SELECT DISTINCT
DISTINCT Removes duplicate rows country FROM
Users;
SELECT name AS
AS Renames column/table (alias) username FROM
Users;

🔸 Join Clauses (Combining Tables)


Clause Purpose Example
SELECT * FROM A
INNER JOIN Returns matching rows from both tables INNER JOIN B ON
A.id = B.a_id;
All rows from left, matching from right , Take
LEFT JOIN SELECT * FROM A
everything from the left table. If the right
(or LEFT LEFT JOIN B ON A.id
table has matching data, bring it too — if not,
OUTER JOIN) = B.a_id;
just leave NULL.

SELECT * FROM A
RIGHT JOIN All rows from right, matching from left RIGHT JOIN B ON
A.id = B.a_id;

FULL OUTER All rows from both tables. SELECT * FROM A


Clause Purpose Example
 It returns all records when there is a match
in either the left or right table.
FULL OUTER JOIN B
JOIN  If there is no match, it fills missing fields
ON A.id = B.a_id;
with NULL.

SELECT * FROM A
CROSS JOIN Returns all combinations (Cartesian product)
CROSS JOIN B;

🔹 Insert/Update Clauses
Clause Purpose Example
INSERT INSERT INTO Users (name, age) VALUES
Adds new row(s)
INTO ('Ali', 20);
VALUES Specifies values to insert VALUES ('Ali', 20);
UPDATE Users SET age = 25 WHERE id =
UPDATE Modifies existing data
1;
Sets column values in
SET SET name = 'John';
UPDATE
Removes rows from a
DELETE DELETE FROM Users WHERE id = 5;
table

🔸 Table Definition Clauses


Clause Purpose Example
CREATE TABLE Users (id INT,
CREATE TABLE Creates a new table
name TEXT);
ALTER TABLE Users ADD age
ALTER TABLE Modifies a table
INT;
ADD Adds column or ADD age INT;
Clause Purpose Example
constraint
Deletes
DROP DROP TABLE Users;
column/table/index
RENAME Renames table/column RENAME TO Customers;
MODIFY / ALTER Changes column
MODIFY age VARCHAR(3);
COLUMN definition

🔹 Constraints Clauses
Clause Purpose Example
PRIMARY Uniquely identifies a
id INT PRIMARY KEY
KEY row
FOREIGN Ensures referential FOREIGN KEY (user_id) REFERENCES
KEY integrity Users(id)
Ensures all values are
UNIQUE email VARCHAR(100) UNIQUE
unique
NOT NULL Disallows NULL values name VARCHAR(50) NOT NULL
Limits values in a
CHECK CHECK (age >= 18)
column
created_at TIMESTAMP DEFAULT
DEFAULT Sets default value
CURRENT_TIMESTAMP

🔸 Transaction Clauses (TCL)


Clause Purpose Example
BEGIN / START TRANSACTION Starts a transaction BEGIN;
COMMIT Saves changes COMMIT;
ROLLBACK Undoes changes ROLLBACK;
Clause Purpose Example
SAVEPOINT Creates a savepoint SAVEPOINT sp1;
RELEASE SAVEPOINT Deletes a savepoint RELEASE SAVEPOINT sp1;

🔹 Access Control Clauses (DCL)


Clause Purpose Example
GRANT Gives permission GRANT SELECT ON Users TO user1;
REVOKE Removes permission REVOKE SELECT ON Users FROM user1;

Pattern :-
Pattern Meaning
'a%' Starts with "a"
'%son' Ends with "son"
'%john
Contains "john" anywhere
%'
Exactly 4 letters where 3rd & 4th are
'__hn'
hn
'j_n%' Starts with "j", any 1 char, then "n"

The GROUP BY clause in SQL is used to group rows that have the
same values in specified columns into summary rows—often
combined with aggregate functions like COUNT(), SUM(), AVG(),
MAX(), or MIN().

BSIC SYNTEX : -
SELECT column_name, AGGREGATE_FUNCTION(column_name)
FROM table_name
GROUP BY column_name;’

Length to calculate length of char.


DATEDIFF return different b/w 2 dates.

You might also like