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

SQL Quries Army ICTO

SQL (Structured Query Language) is a standard language for creating, managing, and querying relational databases, focusing on data storage, retrieval, and integrity. It includes various sub-languages for defining structures (DDL), managing data (DML), controlling access (DCL), and handling transactions (TCL). Key concepts include database management systems (DBMS vs RDBMS), SQL commands for data manipulation, constraints, joins, and normalization to enhance data integrity.

Uploaded by

Mubeen Mustafa
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)
4 views9 pages

SQL Quries Army ICTO

SQL (Structured Query Language) is a standard language for creating, managing, and querying relational databases, focusing on data storage, retrieval, and integrity. It includes various sub-languages for defining structures (DDL), managing data (DML), controlling access (DCL), and handling transactions (TCL). Key concepts include database management systems (DBMS vs RDBMS), SQL commands for data manipulation, constraints, joins, and normalization to enhance data integrity.

Uploaded by

Mubeen Mustafa
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/ 9

What isSQL?

Definition:​
SQL (Structured Query Language) is a standard language used to create, manage, and
query relational databases.

Purpose / logic:

●​ Store, retrieve, update, and delete data​

●​ Manage database structure​

●​ Ensure data integrity​

2. Abbreviations (must-know)
Abbreviation Full Form

SQL Structured Query Language

DBMS Database Management System

RDBMS Relational Database Management


System

DDL Data Definition Language

DML Data Manipulation Language

DCL Data Control Language

TCL Transaction Control Language

PK Primary Key

FK Foreign Key

3. What is DBMS vs RDBMS?


DBMS RDBMS
Data Files Tables

Relation No Yes

Example MS Access (older) MySQL, Oracle, SQL Server

Purpose:​
RDBMS organizes data into related tables → data integrity.

4. SQL sub-languages
Type Purpose Example

DDL Define structure CREATE, ALTER, DROP

DML Manage data SELECT, INSERT, UPDATE, DELETE

DCL Control access GRANT, REVOKE

TCL Manage COMMIT, ROLLBACK, SAVEPOINT


transactions

5. Tables & Data Types


SQL Type Example

INT Whole number

VARCHAR(n) Text of n length

DATE Date

FLOAT / DOUBLE Decimal number

6. CREATE TABLE (DDL)

CREATE TABLE students (


id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
dob DATE
);

7. INSERT (DML)
sql
CopyEdit
INSERT INTO students (id, name, age, dob)
VALUES (1, 'Ali', 21, '2003-01-15');

8. SELECT (DML)
Purpose Query

All rows SELECT * FROM students;

Some SELECT name, age FROM students;


columns

Condition SELECT * FROM students WHERE age


> 20;

Order SELECT * FROM students ORDER BY


age DESC;

Unique values SELECT DISTINCT age FROM


students;

9. UPDATE & DELETE


Action Query

Update UPDATE students SET age=22


WHERE id=1;
Delete row DELETE FROM students WHERE
id=1;

10. ALTER TABLE


Action Query

Add column ALTER TABLE students ADD email


VARCHAR(100);

Drop column ALTER TABLE students DROP COLUMN


email;

Modify column ALTER TABLE students MODIFY age


SMALLINT;

11. Constraints
Constraint Purpose

PRIMARY KEY Uniquely identify row

FOREIGN KEY Reference another


table

UNIQUE Unique value

NOT NULL Cannot be empty

CHECK Validate data

Example:

CREATE TABLE orders (


order_id INT PRIMARY KEY,
student_id INT,
FOREIGN KEY (student_id) REFERENCES students(id)
);
12. Aggregate Functions
Function Example

COUNT() SELECT COUNT(*) FROM


students;

SUM() SELECT SUM(age) FROM


students;

AVG() SELECT AVG(age) FROM


students;

MAX() SELECT MAX(age) FROM


students;

MIN() SELECT MIN(age) FROM


students;

13. GROUP BY & HAVING


Purpose Query

Group SELECT age, COUNT(*) FROM students GROUP BY age;

Filter groups SELECT age, COUNT(*) FROM students GROUP BY age


HAVING COUNT(*)>1;

14. Joins
Type Purpose Query (example)

INNER Rows with match in SELECT * FROM students s INNER JOIN orders
JOIN both o ON s.id=o.student_id;

LEFT JOIN All left + matching SELECT * FROM students s LEFT JOIN orders
right o ON s.id=o.student_id;

RIGHT All right + matching SELECT * FROM students s RIGHT JOIN orders
JOIN left o ON s.id=o.student_id;
FULL JOIN All rows both sides Depends on DB

15. Subquery
Query inside another query:

SELECT name FROM students WHERE id IN (SELECT student_id FROM orders);

16. Views
Definition:​
Virtual table based on SELECT.

Create:

sql

CREATE VIEW student_view AS SELECT name, age FROM students;

Use:

sql

SELECT * FROM student_view;

17. Index
Purpose:​
Speed up data retrieval.

Create:

sql
CopyEdit
CREATE INDEX idx_name ON students(name);
18. Transactions (TCL)
Command Purpose

BEGIN Start
transaction

COMMIT Save changes

ROLLBACK Undo changes

SAVEPOINT Set restore


point

Example:

sql

BEGIN;
UPDATE students SET age=30 WHERE id=2;
ROLLBACK; -- undo change

19. Normalization
Purpose:​
Reduce redundancy & improve integrity.

Normal Form Rule

1NF Atomic values

2NF No partial dependency

3NF No transitive
dependency

20. Difference: DELETE vs TRUNCATE vs DROP


DELETE TRUNCATE DROP

Remove data Yes Yes Table & data

Rollback Yes No No

Table Yes Yes No


remains

21. Wildcards
Symbol Use

% Any number of chars

_ Single char

Example:

sql
CopyEdit
SELECT * FROM students WHERE name LIKE 'A%';

22. BETWEEN, IN, IS NULL


Use Query

Range age BETWEEN 18


AND 25

List id IN (1,2,3)

Null check email IS NULL

23. Stored Procedure


Definition:​
Saved SQL code that can be reused.

Example (syntax depends on DB):


sql

CREATE PROCEDURE GetAllStudents AS SELECT * FROM students;

24. Why learn SQL?


●​ Foundation of all databases​

●​ In-demand skill​

●​ Used by web, data science, software​

●​ Powerful & human-readable​

To remember easily:

●​ DDL: CREATE, ALTER, DROP​

●​ DML: SELECT, INSERT, UPDATE, DELETE​

●​ Aggregate: COUNT, SUM, AVG​

●​ Joins: combine tables​

●​ WHERE filters rows; HAVING filters groups

You might also like