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

SQL Fundamentals

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)
4 views7 pages

SQL Fundamentals

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/ 7

SQL Fundamentals - Beginner-Friendly Guide

🌟 What is SQL?
SQL (Structured Query Language) is the language used to interact with Relational
Databases. It helps in performing tasks like:

 Creating and deleting databases/tables.


 Inserting, updating, and deleting data.
 Retrieving (querying) data based on conditions.

It is used in almost every modern application that stores data, such as banking systems, e-
commerce platforms, and social media sites.

🔍 SQL Categories
SQL is divided into multiple categories:

 DDL: Data Definition Language


 DML: Data Manipulation Language
 DQL: Data Query Language
 DCL: Data Control Language
 TCL: Transaction Control Language

In this guide, we will focus on:

 DDL
 DML
 DQL
 Keys
 Advanced SQL Features
 Aggregate Functions

🔢 DDL Commands (Data Definition Language)


DDL is used to define or change the structure of a database (like tables, columns, etc).

1. CREATE
Used to create new database objects (table, view, index, etc).

Syntax:

CREATE TABLE table_name (


column1 datatype,
column2 datatype,
...
);

Example:

CREATE TABLE Students (


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

2. DROP

Used to delete database objects permanently.

Syntax:

DROP TABLE table_name;

Example:

DROP TABLE Students;

⚠️Be careful! DROP permanently deletes the table and all its data.

3. ALTER

Used to change the structure of an existing table (add/modify/remove columns).

Examples:

 Add a column:

ALTER TABLE Students ADD Email VARCHAR(100);

 Modify column type:

ALTER TABLE Students MODIFY Age SMALLINT;

 Drop a column:

ALTER TABLE Students DROP COLUMN Email;


4. TRUNCATE

Used to delete all rows from a table, but not the table itself.

TRUNCATE TABLE Students;

🔹 Faster than DELETE, but cannot be rolled back.

5. RENAME

Used to rename a table.

RENAME TABLE Students TO Learners;

🔑 SQL Keys
Keys are constraints used to identify and enforce relationships between tables.

1. Primary Key

 Uniquely identifies each row in a table.


 Cannot be NULL or duplicate.

CREATE TABLE Students (


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

2. Candidate Key

 Any column or combination of columns that can uniquely identify a row.


 A table can have multiple candidate keys.

Example: ID, Email can both be candidate keys.

3. Super Key

 Any superset of candidate key that can uniquely identify a row.


 Includes all candidate keys + extra attributes.

4. Foreign Key

 A column that refers to the primary key of another table.


 Maintains referential integrity.
CREATE TABLE Marks (
StudentID INT,
FOREIGN KEY (StudentID) REFERENCES Students(ID)
);

📊 DML Commands (Data Manipulation Language)


Used to modify the actual data inside tables.

1. INSERT

Adds new rows to a table.

INSERT INTO Students (ID, Name, Age) VALUES (1, 'Dharaneesh', 21);

2. UPDATE

Modifies existing rows.

UPDATE Students SET Age = 22 WHERE ID = 1;

3. DELETE

Removes rows from a table.

DELETE FROM Students WHERE ID = 1;

⚠️DELETE can be rolled back (unlike TRUNCATE).

📈 DQL Commands (Data Query Language)


Used to fetch data.

SELECT
SELECT * FROM Students;
SELECT Name, Age FROM Students WHERE Age > 20;

You can use WHERE, ORDER BY, GROUP BY, etc.

🧠 Advanced SQL Clauses


1. ANY

Returns TRUE if any value from a subquery meets the condition.

SELECT Name FROM Students WHERE Age > ANY (SELECT Age FROM Students WHERE Age
< 25);

2. ALL

Returns TRUE if the condition is true for all values.

SELECT Name FROM Students WHERE Age > ALL (SELECT Age FROM Students WHERE Age
< 18);

3. IN

Checks if a value exists in a given list.

SELECT * FROM Students WHERE Age IN (18, 20, 22);

4. EXISTS

Returns TRUE if a subquery returns any rows.

SELECT Name FROM Students S WHERE EXISTS (


SELECT * FROM Marks M WHERE S.ID = M.StudentID
);

5. NOT EXISTS

Returns TRUE if a subquery returns no rows.

SELECT Name FROM Students S WHERE NOT EXISTS (


SELECT * FROM Marks M WHERE S.ID = M.StudentID
);

🔀 Set Operators
1. UNION

Combines results of two SELECTs, removes duplicates.

SELECT Name FROM Students


UNION
SELECT Name FROM Teachers;
2. INTERSECT

Returns rows common to both SELECTs.

SELECT Name FROM Students


INTERSECT
SELECT Name FROM ClubMembers;

(Note: INTERSECT is not supported in MySQL but is in PostgreSQL, SQL Server.)

📊 Aggregate Functions
Used to perform calculations on multiple rows.

Function Description Example


SUM() Total value SELECT SUM(Age) FROM Students;
COUNT() Row count SELECT COUNT(*) FROM Students;
AVG() Average SELECT AVG(Age) FROM Students;
MIN() Minimum SELECT MIN(Age) FROM Students;
MAX() Maximum SELECT MAX(Age) FROM Students;

🔎 EXPLAIN
Used to understand how the SQL engine executes a query (for optimization).

EXPLAIN SELECT * FROM Students WHERE Age > 20;

🧩 COALESCE()
Returns the first non-null value in a list.

SELECT COALESCE(NULL, NULL, 'Dharaneesh', 'Default');


-- Output: Dharaneesh

✅ Practice Setup
CREATE TABLE Students (
ID INT PRIMARY KEY,
Name VARCHAR(100),
Age INT,
Email VARCHAR(100)
);

CREATE TABLE Marks (


StudentID INT,
Subject VARCHAR(50),
Mark INT,
FOREIGN KEY (StudentID) REFERENCES Students(ID)
);

You might also like