Home
Education
D/L Mode
HTML CSS JavaScript Python SQL Node.js PHP Java C++

Welcome to SQL

Start your journey in database management

Lessons

Start learning step by step

Question & Answer

Check your understanding

Interview Asked Questions

Prepare for real interviews

Quiz

Test your skills

πŸ—„οΈ SQL Lesson 1: Introduction to SQL

SQL (Structured Query Language) is used to communicate with databases. It helps you store, retrieve, and manipulate data efficiently.

-- Example: Creating a simple database
CREATE DATABASE School;
    
βœ”

πŸ—„οΈ SQL Lesson 2: Tables and Columns

Tables store data in rows and columns. Each column has a specific data type like INT, VARCHAR, DATE, etc.

-- Create a table
CREATE TABLE Students (
    ID INT PRIMARY KEY,
    Name VARCHAR(50),
    Age INT,
    EnrollmentDate DATE
);
    
βœ”

πŸ—„οΈ SQL Lesson 3: Inserting Data

You can insert data into tables using the INSERT INTO statement.

-- Insert a record
INSERT INTO Students (ID, Name, Age, EnrollmentDate)
VALUES (1, 'Alice', 20, '2026-04-05');
    
βœ”

πŸ—„οΈ SQL Lesson 4: Retrieving Data (SELECT)

The SELECT statement retrieves data from one or more tables.

-- Get all students
SELECT * FROM Students;

-- Get specific columns
SELECT Name, Age FROM Students;
    
βœ”

πŸ—„οΈ SQL Lesson 5: Filtering Data (WHERE)

Use WHERE to filter data based on conditions.

-- Get students older than 18
SELECT * FROM Students
WHERE Age > 18;

-- Get student named Alice
SELECT * FROM Students
WHERE Name = 'Alice';
    
βœ”

πŸ—„οΈ SQL Lesson 6: Sorting Data (ORDER BY)

Use ORDER BY to sort results in ascending (ASC) or descending (DESC) order.

-- Get students sorted by age ascending
SELECT * FROM Students
ORDER BY Age ASC;

-- Get students sorted by name descending
SELECT * FROM Students
ORDER BY Name DESC;
    
βœ”

πŸ—„οΈ SQL Lesson 7: Updating Data (UPDATE)

Use UPDATE to modify existing records in a table.

-- Update Alice's age
UPDATE Students
SET Age = 21
WHERE Name = 'Alice';
    
βœ”

πŸ—„οΈ SQL Lesson 8: Deleting Data (DELETE)

Use DELETE to remove records from a table.

-- Delete student named Alice
DELETE FROM Students
WHERE Name = 'Alice';
    
βœ”

πŸ—„οΈ SQL Lesson 9: Limiting Results (LIMIT)

Use LIMIT to restrict the number of rows returned by a query.

-- Get only the first 3 students
SELECT * FROM Students
LIMIT 3;
    
βœ”

πŸ—„οΈ SQL Lesson 10: Using DISTINCT

DISTINCT removes duplicate values from the results.

-- Get unique ages of students
SELECT DISTINCT Age FROM Students;
    
βœ”

πŸ—„οΈ SQL Lesson 11: SQL Functions - COUNT()

COUNT() counts the number of rows that match a condition.

-- Count all students
SELECT COUNT(*) FROM Students;

-- Count students older than 18
SELECT COUNT(*) FROM Students
WHERE Age > 18;
    
βœ”

πŸ—„οΈ SQL Lesson 12: SQL Functions - AVG(), SUM(), MIN(), MAX()

These functions calculate averages, totals, minimum, and maximum values.

-- Average age
SELECT AVG(Age) FROM Students;

-- Sum of ages
SELECT SUM(Age) FROM Students;

-- Minimum and maximum age
SELECT MIN(Age), MAX(Age) FROM Students;
    
βœ”

πŸ—„οΈ SQL Lesson 13: GROUP BY

GROUP BY groups rows that have the same values in a column for aggregate functions.

-- Count students by age
SELECT Age, COUNT(*) 
FROM Students
GROUP BY Age;
    
βœ”

πŸ—„οΈ SQL Lesson 14: HAVING

HAVING filters groups after aggregation (unlike WHERE, which filters rows before grouping).

-- Count students by age, only ages with more than 1 student
SELECT Age, COUNT(*) 
FROM Students
GROUP BY Age
HAVING COUNT(*) > 1;
    
βœ”

πŸ—„οΈ SQL Lesson 15: Aliases (AS)

Aliases rename columns or tables in the result for readability.

-- Rename column in result
SELECT Name AS StudentName, Age AS StudentAge
FROM Students;

-- Alias a table
SELECT s.Name, s.Age
FROM Students AS s;
    
βœ”

πŸ—„οΈ SQL Lesson 16: INNER JOIN

INNER JOIN combines rows from two tables where there is a match in both tables.

-- Example tables: Students and Courses
SELECT Students.Name, Courses.CourseName
FROM Students
INNER JOIN Courses
ON Students.ID = Courses.StudentID;
    
βœ”

πŸ—„οΈ SQL Lesson 17: LEFT JOIN

LEFT JOIN returns all rows from the left table, and matched rows from the right table. If no match, NULL is returned.

SELECT Students.Name, Courses.CourseName
FROM Students
LEFT JOIN Courses
ON Students.ID = Courses.StudentID;
    
βœ”

πŸ—„οΈ SQL Lesson 18: RIGHT JOIN

RIGHT JOIN returns all rows from the right table, and matched rows from the left table. If no match, NULL is returned.

SELECT Students.Name, Courses.CourseName
FROM Students
RIGHT JOIN Courses
ON Students.ID = Courses.StudentID;
    
βœ”

πŸ—„οΈ SQL Lesson 19: FULL OUTER JOIN

FULL OUTER JOIN returns all rows when there is a match in either left or right table. NULLs appear when there is no match.

SELECT Students.Name, Courses.CourseName
FROM Students
FULL OUTER JOIN Courses
ON Students.ID = Courses.StudentID;
    
βœ”

πŸ—„οΈ SQL Lesson 20: UNION

UNION combines results of two SELECT queries into a single result. Duplicate rows are removed unless UNION ALL is used.

-- Combine two queries
SELECT Name FROM Students
UNION
SELECT Name FROM Teachers;

-- Use UNION ALL to keep duplicates
SELECT Name FROM Students
UNION ALL
SELECT Name FROM Teachers;
    
βœ”

πŸ—„οΈ SQL Lesson 21: LIKE Operator

Use LIKE to search for a pattern in a column.

-- Find students whose name starts with 'A'
SELECT * FROM Students
WHERE Name LIKE 'A%';

-- Find students whose name contains 'li'
SELECT * FROM Students
WHERE Name LIKE '%li%';
    
βœ”

πŸ—„οΈ SQL Lesson 22: IN Operator

IN checks if a value matches any value in a list.

-- Get students with ID 1, 2, or 3
SELECT * FROM Students
WHERE ID IN (1, 2, 3);
    
βœ”

πŸ—„οΈ SQL Lesson 23: BETWEEN Operator

BETWEEN filters data within a range.

-- Get students aged between 18 and 22
SELECT * FROM Students
WHERE Age BETWEEN 18 AND 22;
    
βœ”

πŸ—„οΈ SQL Lesson 24: IS NULL / IS NOT NULL

Check for NULL values using IS NULL or IS NOT NULL.

-- Find students with no age recorded
SELECT * FROM Students
WHERE Age IS NULL;

-- Find students with an age
SELECT * FROM Students
WHERE Age IS NOT NULL;
    
βœ”

πŸ—„οΈ SQL Lesson 25: CASE Statement

CASE allows conditional logic in SELECT queries.

-- Label students as Adult or Minor
SELECT Name, Age,
CASE 
    WHEN Age >= 18 THEN 'Adult'
    ELSE 'Minor'
END AS Status
FROM Students;
    
βœ”

πŸ—„οΈ SQL Lesson 26: Subqueries

Subqueries are queries inside another query to filter or calculate data.

-- Get students older than the average age
SELECT * FROM Students
WHERE Age > (SELECT AVG(Age) FROM Students);
    
βœ”

πŸ—„οΈ SQL Lesson 27: EXISTS

EXISTS checks if a subquery returns any rows.

-- Get students who have enrolled in courses
SELECT * FROM Students s
WHERE EXISTS (
    SELECT 1 FROM Courses c
    WHERE c.StudentID = s.ID
);
    
βœ”

πŸ—„οΈ SQL Lesson 28: Indexes

Indexes improve query performance by allowing faster searches.

-- Create an index on the Name column
CREATE INDEX idx_name
ON Students(Name);
    
βœ”

πŸ—„οΈ SQL Lesson 29: Views

Views are virtual tables created from a SELECT query.

-- Create a view for adult students
CREATE VIEW AdultStudents AS
SELECT Name, Age
FROM Students
WHERE Age >= 18;

-- Query the view
SELECT * FROM AdultStudents;
    
βœ”

πŸ—„οΈ SQL Lesson 30: Transactions

Transactions ensure a set of operations are completed together or rolled back in case of error.

-- Start a transaction
BEGIN;

-- Update multiple records
UPDATE Students SET Age = Age + 1 WHERE ID = 1;
UPDATE Students SET Age = Age + 1 WHERE ID = 2;

-- Commit changes
COMMIT;

-- Or rollback if something goes wrong
ROLLBACK;
    
πŸŽ‰ Congratulations! You have completed all SQL lessons!
Keep practicing and building amazing Applications!

πŸ’‘ Questions & Answers

❓ What is SQL?
SQL (Structured Query Language) is a standard language used to communicate with and manipulate relational databases.
❓ What are the different types of SQL commands?
SQL commands are categorized into: DDL (Data Definition Language), DML (Data Manipulation Language), DCL (Data Control Language), and TCL (Transaction Control Language).
❓ What is a primary key in SQL?
A primary key is a column or combination of columns that uniquely identifies each row in a table. It cannot contain NULL values and must be unique.
❓ What is a foreign key in SQL?
A foreign key is a column in one table that refers to the primary key of another table, establishing a relationship between the two tables.
❓ What is the difference between `INNER JOIN` and `LEFT JOIN`?
`INNER JOIN` returns only the matching rows between tables, while `LEFT JOIN` returns all rows from the left table and matching rows from the right table, with NULLs for non-matching rows.
❓ What is the difference between `WHERE` and `HAVING` in SQL?
`WHERE` filters rows before grouping, while `HAVING` filters groups after aggregation using functions like COUNT(), SUM(), etc.
❓ What is a SQL index?
An index is a database object that improves the speed of data retrieval from a table at the cost of additional storage and slower writes.
❓ What is the difference between `UNION` and `UNION ALL`?
`UNION` combines result sets from multiple queries and removes duplicates. `UNION ALL` combines result sets and keeps duplicates.
❓ What is normalization in SQL?
Normalization is the process of organizing data in tables to reduce redundancy and improve data integrity. It involves applying normal forms (1NF, 2NF, 3NF, etc.).
❓ What is denormalization in SQL?
Denormalization is the process of combining tables or adding redundant data to improve query performance at the expense of data redundancy.
❓ What is a SQL view?
A view is a virtual table based on the result of a SQL query. It does not store data itself but provides a way to look at data from one or more tables.
❓ What is the difference between `DELETE`, `TRUNCATE`, and `DROP`?
`DELETE` removes rows and can be rolled back. `TRUNCATE` removes all rows quickly without logging individual row deletions. `DROP` deletes the entire table structure permanently.
❓ What is a stored procedure in SQL?
A stored procedure is a precompiled SQL program stored in the database. It can accept parameters and execute complex operations efficiently.
❓ What is a trigger in SQL?
A trigger is a database object that automatically executes a specified SQL action when certain events occur on a table, like INSERT, UPDATE, or DELETE.
❓ What is the difference between `CHAR` and `VARCHAR` in SQL?
`CHAR` is a fixed-length string, padding unused spaces. `VARCHAR` is a variable-length string, storing only the entered characters.
❓ What is a primary key and can a table have multiple primary keys?
A primary key uniquely identifies each row in a table. A table can have only one primary key, which can consist of multiple columns (composite key).
❓ What is a foreign key and why is it used?
A foreign key is a column that links to the primary key of another table. It is used to maintain referential integrity between related tables.
❓ What is the difference between `INNER JOIN`, `LEFT JOIN`, `RIGHT JOIN`, and `FULL OUTER JOIN`?
`INNER JOIN` returns matching rows. `LEFT JOIN` returns all rows from the left table and matching rows from the right. `RIGHT JOIN` returns all rows from the right table and matching rows from the left. `FULL OUTER JOIN` returns all rows from both tables, with NULLs for unmatched rows.
❓ What are aggregate functions in SQL?
Aggregate functions perform calculations on multiple rows and return a single value. Examples include `COUNT()`, `SUM()`, `AVG()`, `MAX()`, and `MIN()`.
❓ What are aggregate functions in SQL?
Aggregate functions perform calculations on multiple rows and return a single value. Examples include `COUNT()`, `SUM()`, `AVG()`, `MAX()`, and `MIN()`.
❓ What is the difference between `UNION` and `JOIN` in SQL?
`UNION` combines results of two queries with the same columns into a single result set. `JOIN` combines columns from two tables based on a related column.
❓ What is a subquery in SQL?
A subquery is a query nested inside another query. It can be used in SELECT, INSERT, UPDATE, or DELETE statements to provide intermediate results.
❓ What is a composite key in SQL?
A composite key is a primary key made up of two or more columns to uniquely identify each row in a table.
❓ What is a SQL transaction?
A transaction is a sequence of SQL operations executed as a single unit. It follows the ACID properties (Atomicity, Consistency, Isolation, Durability) to ensure data integrity.
❓ What is a SQL transaction?
A transaction is a sequence of SQL operations executed as a single unit. It follows the ACID properties (Atomicity, Consistency, Isolation, Durability) to ensure data integrity.
❓ What is the difference between `TRUNCATE` and `DELETE`?
`DELETE` removes rows one by one and can be rolled back. `TRUNCATE` removes all rows quickly without logging individual row deletions and cannot be rolled back in most databases.
❓ What is a SQL index and why is it used?
An index is a database object that improves query performance by allowing faster data retrieval. It can slow down writes and consumes extra storage.
❓ What is a SQL index and why is it used?
An index is a database object that improves query performance by allowing faster data retrieval. It can slow down writes and consumes extra storage.
❓ What is a trigger in SQL?
A trigger is a database object that automatically executes a specified action (INSERT, UPDATE, DELETE) when certain events occur on a table.
❓ What is the difference between `RANK()`, `DENSE_RANK()`, and `ROW_NUMBER()`?
`ROW_NUMBER()` assigns a unique sequential number to each row. `RANK()` gives the same rank to ties and skips subsequent ranks. `DENSE_RANK()` gives the same rank to ties but does not skip ranks.

🎯 SQL Interview Questions

❓ What is SQL?
SQL (Structured Query Language) is a standard language used to communicate with and manage relational databases, allowing you to create, read, update, and delete data.
❓ What are the different types of SQL commands?
SQL commands are categorized as:
  • DDL (Data Definition Language): CREATE, ALTER, DROP
  • DML (Data Manipulation Language): SELECT, INSERT, UPDATE, DELETE
  • DCL (Data Control Language): GRANT, REVOKE
  • TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT
❓ What is a primary key?
A primary key is a column or set of columns that uniquely identifies each row in a table. It cannot contain NULL values and must be unique.
❓ What is a foreign key?
A foreign key is a column in one table that references the primary key of another table, establishing a relationship between the two tables and ensuring referential integrity.
❓ What is the difference between INNER JOIN and LEFT JOIN?
INNER JOIN: Returns only matching rows from both tables.
LEFT JOIN: Returns all rows from the left table and matching rows from the right table; unmatched right table rows return NULL.
❓ What is normalization?
Normalization is the process of organizing tables and columns to reduce data redundancy and improve data integrity. It involves applying normal forms like 1NF, 2NF, 3NF, etc.
❓ What is the difference between DELETE, TRUNCATE, and DROP?
DELETE: Removes rows one by one; can be rolled back.
TRUNCATE: Removes all rows quickly; cannot be rolled back in most databases.
DROP: Deletes the entire table or database permanently.
❓ What is an index in SQL?
An index is a database object that speeds up data retrieval. It creates a lookup table for faster queries but can slow down INSERT, UPDATE, and DELETE operations.
❓ What are aggregate functions in SQL?
Aggregate functions perform calculations on multiple rows and return a single value. Examples include COUNT(), SUM(), AVG(), MAX(), and MIN().
❓ What is a subquery in SQL?
A subquery is a query nested inside another query. It can be used in SELECT, INSERT, UPDATE, or DELETE statements to provide intermediate results for filtering or calculation.

πŸ“ SQL Quiz: Test Your Knowledge

1. What does SQL stand for?

2. Which SQL statement is used to fetch data from a table?

3. Which statement is used to insert data into a table?

4. Which SQL keyword is used to remove a table?

5. Which clause is used to filter the records in a SELECT query?

6. Which SQL statement is used to update data in a table?

7. Which SQL statement deletes all records from a table but keeps the table?

8. Which SQL clause is used to group rows that have the same values?

9. Which command is used to remove a record from a table?

10. Which keyword is used to sort the result set?

11. Which SQL keyword is used to combine results from two queries?

12. Which SQL function returns the number of rows?

13. Which statement is used to create a new table?

14. Which keyword is used to select only unique values?

×