SQL
SQL is a short-form of the structured query language, and it is pronounced as S-Q-L or
sometimes as See-Quell.
This database language is mainly designed for maintaining the data in relational database
management systems. It is a special tool used by data professionals for handling structured
data (data which is stored in the form of tables). It is also designed for stream processing in
RDSMS.
You can easily create and manipulate the database, access and modify the table rows and
columns, etc. This query language became the standard of ANSI in the year of 1986 and ISO
in the year of 1987.
If you want to get a job in the field of data science, then it is the most important query
language to learn. Big enterprises like Facebook, Instagram, and LinkedIn, use SQL for storing
the data in the back-end.
How SQL Works?
Basic SQL Commands
The SQL commands help in creating and managing the database. The most common SQL
commands which are highly used are mentioned below:
This command helps in accessing the single or multiple rows from one or multiple tables of
the database. We can also use this command with the WHERE clause.
DDL Commands
1. CREATE TABLE LibraryBooks
CREATE TABLE LibraryBooks (
BookID INT,
Title VARCHAR(100),
Author VARCHAR(100),
Genre VARCHAR(50),
PublishedYear INT,
Available BOOLEAN
);
🔹 Output:
BookID Title Author Genre PublishedYear Available
Table LibraryBooks is created with six columns.
2. ALTER TABLE LibraryBooks ADD Publisher VARCHAR(100);
ALTER TABLE LibraryBooks ADD Publisher VARCHAR(100);
🔹 Output:
BookID Title Author Genre PublishedYear Available Publisher
Column Publisher is added to the table.
3. DROP TABLE LibraryBooks;
DROP TABLE LibraryBooks;
🔹 Output:
The LibraryBooks table is permanently removed from the database. No table exists now.
4. TRUNCATE TABLE LibraryBooks;
TRUNCATE TABLE LibraryBooks;
🔹 Output:
BookID Title Author Genre PublishedYear Available Publisher
All rows are deleted, but the table structure remains.
5. RENAME TABLE LibraryBooks TO BooksCollection;
RENAME TABLE LibraryBooks TO BooksCollection;
🔹 Output:
The table name is now BooksCollection with the same structure:
BookID Title Author Genre PublishedYear Available Publisher
DML Commands
1. INSERT INTO LibraryBooks (Single Row)
INSERT INTO LibraryBooks (BookID, Title, Author, Genre, PublishedYear, Available)
VALUES (1, 'The Hobbit', 'J.R.R. Tolkien', 'Fantasy', 1937, TRUE);
🔹 Output:
BookID Title Author Genre PublishedYear Available
J.R.R.
1 The Hobbit Fantasy 1937 TRUE
Tolkien
2. INSERT Multiple Records
INSERT INTO LibraryBooks (BookID, Title, Author, Genre, PublishedYear, Available)
VALUES
(2, 'Inferno', 'Dan Brown', 'Thriller', 2013, TRUE),
(3, '1984', 'George Orwell', 'Dystopian', 1949, FALSE);
🔹 Output:
BookID Title Author Genre PublishedYear Available
1 The Hobbit J.R.R. Tolkien Fantasy 1937 TRUE
2 Inferno Dan Brown Thriller 2013 TRUE
3 1984 George Orwell Dystopian 1949 FALSE
3. SELECT * FROM LibraryBooks
SELECT * FROM LibraryBooks;
🔹 Output:
BookID Title Author Genre PublishedYear Available
1 The Hobbit J.R.R. Tolkien Fantasy 1937 TRUE
2 Inferno Dan Brown Thriller 2013 TRUE
3 1984 George Orwell Dystopian 1949 FALSE
4. SELECT Title, Author FROM LibraryBooks
SELECT Title, Author FROM LibraryBooks;
🔹 Output:
Title Author
The Hobbit J.R.R. Tolkien
Inferno Dan Brown
1984 George Orwell
5. SELECT * WHERE Genre = 'Thriller'
SELECT * FROM LibraryBooks WHERE Genre = 'Thriller';
🔹 Output:
BookID Title Author Genre PublishedYear Available
2 Inferno Dan Brown Thriller 2013 TRUE
6. SELECT * WHERE Genre = 'Fantasy' AND Available = TRUE
SELECT * FROM LibraryBooks WHERE Genre = 'Fantasy' AND Available = TRUE;
🔹 Output:
BookID Title Author Genre PublishedYear Available
1 The Hobbit J.R.R. Tolkien Fantasy 1937 TRUE
7. SELECT * WHERE Genre = 'Fantasy' OR PublishedYear > 2000
SELECT * FROM LibraryBooks WHERE Genre = 'Fantasy' OR PublishedYear > 2000;
🔹 Output:
BookID Title Author Genre PublishedYear Available
1 The Hobbit J.R.R. Tolkien Fantasy 1937 TRUE
2 Inferno Dan Brown Thriller 2013 TRUE
8. SELECT * WHERE PublishedYear BETWEEN 1900 AND 2000
SELECT * FROM LibraryBooks WHERE PublishedYear BETWEEN 1900 AND 2000;
🔹 Output:
BookID Title Author Genre PublishedYear Available
1 The Hobbit J.R.R. Tolkien Fantasy 1937 TRUE
3 1984 George Orwell Dystopian 1949 FALSE
9. SELECT * WHERE Genre IN ('Fantasy', 'Dystopian')
SELECT * FROM LibraryBooks WHERE Genre IN ('Fantasy', 'Dystopian');
🔹 Output:
BookID Title Author Genre PublishedYear Available
1 The Hobbit J.R.R. Tolkien Fantasy 1937 TRUE
3 1984 George Orwell Dystopian 1949 FALSE
10. SELECT * WHERE Title LIKE 'T%'
SELECT * FROM LibraryBooks WHERE Title LIKE 'T%';
🔹 Output:
BookID Title Author Genre PublishedYear Available
1 The Hobbit J.R.R. Tolkien Fantasy 1937 TRUE
11. SELECT * FROM LibraryBooks LIMIT 2
SELECT * FROM LibraryBooks LIMIT 2;
🔹 Output:
BookID Title Author Genre PublishedYear Available
1 The Hobbit J.R.R. Tolkien Fantasy 1937 TRUE
2 Inferno Dan Brown Thriller 2013 TRUE
12. UPDATE LibraryBooks SET Available = FALSE WHERE BookID = 2
UPDATE LibraryBooks SET Available = FALSE WHERE BookID = 2;
🔹 Output:
BookID Title Author Genre PublishedYear Available
2 Inferno Dan Brown Thriller 2013 FALSE
13. DELETE FROM LibraryBooks WHERE BookID = 2
DELETE FROM LibraryBooks WHERE BookID = 2;
🔹 Output:
Book with ID 2 ("Inferno") is removed.
BookID Title Author Genre PublishedYear Available
1 The Hobbit J.R.R. Tolkien Fantasy 1937 TRUE
3 1984 George Orwell Dystopian 1949 FALSE
TCL Commands
1. START TRANSACTION & COMMIT
START TRANSACTION;
INSERT INTO LibraryBooks (BookID, Title, Author, Genre, PublishedYear, Available)
VALUES (4, 'Pride and Prejudice', 'Jane Austen', 'Romance', 1813, TRUE);
UPDATE LibraryBooks SET Available = FALSE WHERE BookID = 1;
COMMIT;
🔹 Output:
All changes are permanently saved to the database.
BookID Title Author Genre PublishedYear Available
1 The Hobbit J.R.R. Tolkien Fantasy 1937 FALSE
3 1984 George Orwell Dystopian 1949 FALSE
4 Pride and Prejudice Jane Austen Romance 1813 TRUE
2. START TRANSACTION & ROLLBACK
START TRANSACTION;
DELETE FROM LibraryBooks WHERE BookID = 3;
ROLLBACK;
🔹 Output:
No changes are applied. The deletion of BookID 3 is undone.
BookID Title Author Genre PublishedYear Available
1 The Hobbit J.R.R. Tolkien Fantasy 1937 FALSE
3 1984 George Orwell Dystopian 1949 FALSE
4 Pride and Prejudice Jane Austen Romance 1813 TRUE
3. SAVEPOINT and ROLLBACK TO SAVEPOINT
START TRANSACTION;
SAVEPOINT BeforeInsert;
INSERT INTO LibraryBooks (BookID, Title, Author, Genre, PublishedYear, Available)
VALUES (5, 'The Da Vinci Code', 'Dan Brown', 'Thriller', 2003, TRUE);
ROLLBACK TO BeforeInsert;
COMMIT;
🔹 Output:
BookID 5 is not added, as we rolled back to the savepoint. Remaining transactions (if any)
are committed.
BookID Title Author Genre PublishedYear Available
1 The Hobbit J.R.R. Tolkien Fantasy 1937 FALSE
3 1984 George Orwell Dystopian 1949 FALSE
4 Pride and Prejudice Jane Austen Romance 1813 TRUE
4. COMMIT without ROLLBACK
START TRANSACTION;
DELETE FROM LibraryBooks WHERE BookID = 4;
COMMIT;
🔹 Output:
BookID 4 is permanently removed from the table.
BookID Title Author Genre PublishedYear Available
1 The Hobbit J.R.R. Tolkien Fantasy 1937 FALSE
3 1984 George Orwell Dystopian 1949 FALSE
DCL Commands
DCL is used to control access to data stored in a database. It is mainly used by database
administrators.
1. GRANT Privileges to a User
GRANT SELECT, INSERT ON LibraryBooks TO 'librarian'@'localhost';
🔹 Output:
The user librarian can now perform SELECT and INSERT operations on the LibraryBooks
table.
2. REVOKE Privileges from a User
REVOKE INSERT ON LibraryBooks FROM 'librarian'@'localhost';
🔹 Output:
The user librarian can no longer insert records into the LibraryBooks table but can still select
(read) data.
DQL Commands
1. SELECT * FROM LibraryBooks
SELECT * FROM LibraryBooks;
🔹 Output:
BookID Title Author Genre PublishedYear Available
1 The Hobbit J.R.R. Tolkien Fantasy 1937 FALSE
3 1984 George Orwell Dystopian 1949 FALSE
2. SELECT Title, Author FROM LibraryBooks
SELECT Title, Author FROM LibraryBooks;
🔹 Output:
Title Author
The Hobbit J.R.R. Tolkien
1984 George Orwell
3. SELECT * WHERE Available = TRUE
SELECT * FROM LibraryBooks WHERE Available = TRUE;
🔹 Output:
No rows returned (based on current state where all books are unavailable).
4. SELECT * WHERE Genre = 'Fantasy'
SELECT * FROM LibraryBooks WHERE Genre = 'Fantasy';
🔹 Output:
BookID Title Author Genre PublishedYear Available
1 The Hobbit J.R.R. Tolkien Fantasy 1937 FALSE
5. SELECT * WHERE PublishedYear > 1950
SELECT * FROM LibraryBooks WHERE PublishedYear > 1950;
🔹 Output:
(No books currently meet this condition — add more to test further.)
SQL Clauses
1. WHERE Clause
SELECT * FROM LibraryBooks WHERE Genre = 'Fantasy';
🔹 Output:
BookID Title Author Genre PublishedYear Available
1 The Hobbit J.R.R. Tolkien Fantasy 1937 FALSE
2. ORDER BY Clause
SELECT * FROM LibraryBooks ORDER BY PublishedYear DESC;
🔹 Output: Sorted books from latest to oldest.
BookID Title Author Genre PublishedYear Available
3 1984 George Orwell Dystopian 1949 FALSE
1 The Hobbit J.R.R. Tolkien Fantasy 1937 FALSE
3. GROUP BY Clause
Let’s say we want to count how many books exist in each genre:
SELECT Genre, COUNT(*) AS TotalBooks
FROM LibraryBooks
GROUP BY Genre;
🔹 Output:
Genre TotalBooks
Fantasy 1
Dystopian 1
4. HAVING Clause
Used after GROUP BY to filter groups:
SELECT Genre, COUNT(*) AS TotalBooks
FROM LibraryBooks
GROUP BY Genre
HAVING COUNT(*) > 0;
🔹 Output: Shows genres having at least one book.
5. LIMIT Clause
SELECT * FROM LibraryBooks LIMIT 1;
🔹 Output: Returns only the first row.
BookID Title Author Genre PublishedYear Available
1 The Hobbit J.R.R. Tolkien Fantasy 1937 FALSE
SQL Joins
We’ll now add another table called Authors:
📘 Authors Table
AuthorID AuthorName Country
1 J.R.R. Tolkien UK
2 George Orwell UK
3 Dan Brown USA
📗 LibraryBooks (Extended with AuthorID)
BookID Title AuthorID Genre PublishedYear Available
1 The Hobbit 1 Fantasy 1937 FALSE
3 1984 2 Dystopian 1949 FALSE
1. INNER JOIN
SELECT LibraryBooks.Title, Authors.AuthorName, Authors.Country
FROM LibraryBooks
INNER JOIN Authors ON LibraryBooks.AuthorID = Authors.AuthorID;
🔹 Output:
Title AuthorName Country
The Hobbit J.R.R. Tolkien UK
1984 George Orwell UK
2. LEFT JOIN
SELECT LibraryBooks.Title, Authors.AuthorName
FROM LibraryBooks
LEFT JOIN Authors ON LibraryBooks.AuthorID = Authors.AuthorID;
🔹 Output:
Returns all books, even if author details are missing.
3. RIGHT JOIN
SELECT LibraryBooks.Title, Authors.AuthorName
FROM LibraryBooks
RIGHT JOIN Authors ON LibraryBooks.AuthorID = Authors.AuthorID;
🔹 Output:
Returns all authors, even if they haven't written any books in the table.
4. FULL OUTER JOIN (If supported)
SELECT LibraryBooks.Title, Authors.AuthorName
FROM LibraryBooks
FULL OUTER JOIN Authors ON LibraryBooks.AuthorID = Authors.AuthorID;
🔹 Output:
Combines results of both left and right joins (not supported in MySQL but works in
PostgreSQL).
ORDER BY
What is ORDER BY?
The ORDER BY clause is used to sort the result set by one or more columns. It defaults to
ascending (ASC) order but can also be used with descending (DESC).
1. ORDER BY PublishedYear (Ascending – Default)
SELECT * FROM LibraryBooks ORDER BY PublishedYear;
🔹 Output: Sorted from oldest to newest.
BookID Title Author Genre PublishedYear Available
1 The Hobbit J.R.R. Tolkien Fantasy 1937 FALSE
3 1984 George Orwell Dystopian 1949 FALSE
2. ORDER BY PublishedYear DESC (Descending)
SELECT * FROM LibraryBooks ORDER BY PublishedYear DESC;
🔹 Output: Sorted from newest to oldest.
BookID Title Author Genre PublishedYear Available
3 1984 George Orwell Dystopian 1949 FALSE
1 The Hobbit J.R.R. Tolkien Fantasy 1937 FALSE
3. ORDER BY Title ASC
SELECT * FROM LibraryBooks ORDER BY Title ASC;
🔹 Output: Sorted alphabetically by title (A to Z).
BookID Title Author Genre PublishedYear Available
3 1984 George Orwell Dystopian 1949 FALSE
1 The Hobbit J.R.R. Tolkien Fantasy 1937 FALSE
4. ORDER BY Genre DESC, Title ASC
SELECT * FROM LibraryBooks ORDER BY Genre DESC, Title ASC;
🔹 Output: Sorted by genre (Z–A), and within each genre, by title (A–Z).
BookID Title Author Genre PublishedYear Available
3 1984 George Orwell Dystopian 1949 FALSE
1 The Hobbit J.R.R. Tolkien Fantasy 1937 FALSE