🔷 What is SQL?
SQL (Structured Query Language) is a standard language used to store, manage, and retrieve data in a relational database.
Popular SQL databases include:
- MySQL
- PostgreSQL
- SQL Server
- Oracle
- SQLite
🔷 What SQL is Used For
SQL helps you:
- Create databases and tables
- Insert data
- Update or delete data
- Search/filter data
- Join multiple tables
- Control access to data
🔷 Most Common SQL Commands
1️⃣ CREATE – Create a table
CREATE TABLE Students (
ID INT,
Name VARCHAR(50),
Age INT
);
2️⃣ INSERT – Add data to table
INSERT INTO Students (ID, Name, Age)
VALUES (1, 'Amit', 20);
3️⃣ SELECT – Retrieve data
SELECT * FROM Students;
4️⃣ WHERE – Filter results
SELECT * FROM Students
WHERE Age > 18;
5️⃣ UPDATE – Modify data
UPDATE Students
SET Age = 21
WHERE ID = 1;
6️⃣ DELETE – Remove data
DELETE FROM Students
WHERE ID = 1;
🔷 Basic SQL Concepts
| Concept | Meaning |
|---|---|
| Table | Collection of rows and columns |
| Row | A single record |
| Column | Field/attribute |
| Primary Key | Unique identifier for each row |
| Foreign Key | Links tables together |
| Join | Combine data from multiple tables |
🔷 Example: JOIN
SELECT Orders.OrderID, Customers.Name
FROM Orders
JOIN Customers
ON Orders.CustomerID = Customers.ID;
Great! Here is More SQL content, including joins, functions, constraints, and practice questions—perfect for beginners to intermediate learners.
🔷 1. SQL JOINS (Very Important)
Joins are used to combine data from multiple tables.
✅ INNER JOIN
Returns matching rows from both tables.
SELECT s.Name, c.CourseName
FROM Students s
INNER JOIN Courses c
ON s.CourseID = c.CourseID;
✅ LEFT JOIN
Returns all rows from left table + matching rows from right.
SELECT s.Name, c.CourseName
FROM Students s
LEFT JOIN Courses c
ON s.CourseID = c.CourseID;
✅ RIGHT JOIN
Returns all rows from right table + matching rows from left.
SELECT s.Name, c.CourseName
FROM Students s
RIGHT JOIN Courses c
ON s.CourseID = c.CourseID;
✅ FULL OUTER JOIN
Returns all rows when there is a match in either table.
SELECT s.Name, c.CourseName
FROM Students s
FULL OUTER JOIN Courses c
ON s.CourseID = c.CourseID;
🔷 2. SQL Constraints
Constraints ensure rules in a table.
Constraint
Description
PRIMARY KEY
Unique + Not Null
FOREIGN KEY
Links tables
UNIQUE
No duplicate values
NOT NULL
Cannot be empty
CHECK
Ensures a condition
DEFAULT
Assigns default value
Example:
CREATE TABLE Employees (
EmpID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Age INT CHECK (Age > 18),
City VARCHAR(50) DEFAULT 'Unknown'
);
🔷 3. SQL Aggregate Functions
Used to perform calculations on data.
Function
Meaning
COUNT()
Counts rows
SUM()
Adds values
AVG()
Average
MIN()
Minimum
MAX()
Maximum
Example:
SELECT COUNT(*), AVG(Age)
FROM Students;
🔷 4. SQL GROUP BY & HAVING
GROUP BY
Used to group rows that have the same values.
SELECT City, COUNT(*)
FROM Students
GROUP BY City;
HAVING
Used for filtering groups (not rows).
SELECT City, COUNT(*)
FROM Students
GROUP BY City
HAVING COUNT(*) > 2;
🔷 5. SQL ORDER BY
SELECT * FROM Students
ORDER BY Age DESC;
🔷 6. SQL Subqueries
Subquery inside another query.
SELECT Name
FROM Students
WHERE Age > (SELECT AVG(Age) FROM Students);
🔷 7. SQL Normalization (Basics)
Normalization reduces redundancy.
Normal Form
Meaning
1NF
No repeating groups, atomic values
2NF
No partial dependency
3NF
No transitive dependency
🔷 8. SQL Practice Questions (Very Useful)
Try these yourself:
Q1. Display employee name and salary who earns more than average salary.
Q2. List customers who have never placed an order.
Q3. Find the second highest salary.
Q4. Count number of students in each city.
Q5. Write a query to delete duplicate records.
.jpg)
0 Comments