SQL Queries: Complete Guide for DBMS Preparation (2025)
SQL (Structured Query Language) is the standard language used to interact with databases. In aptitude tests, technical interviews, and competitive exams, SQL Queries are frequently asked to test a candidate’s ability to retrieve, insert, update, and manage data.
This guide covers the important SQL concepts, query patterns, solved examples, and MCQs for your DBMS preparation.
What are SQL Queries?
An SQL Query is a request made to a database to retrieve or manipulate information.
- SQL allows users to perform CRUD operations: Create, Read, Update, Delete.
- Works with relational databases like MySQL, Oracle, PostgreSQL, SQL Server.
Types of SQL Queries
DDL (Data Definition Language)
CREATE, ALTER, DROP, TRUNCATE
Defines and modifies database schema.
DML (Data Manipulation Language)
INSERT, UPDATE, DELETE
Used for data modification.
DQL (Data Query Language)
SELECT
Used to retrieve data from tables.
DCL (Data Control Language)
GRANT, REVOKE
Manages database permissions.
TCL (Transaction Control Language)
COMMIT, ROLLBACK, SAVEPOINT
Manages transactions in a database.
Common SQL Queries
Create Table:
CREATE TABLE Students ( ID INT PRIMARY KEY, Name VARCHAR(50), Marks INT );
Insert Data:
INSERT INTO Students (ID, Name, Marks) VALUES (1, 'Amit', 85);
Select Query:
SELECT Name, Marks FROM Students WHERE Marks > 80;
Update Data:
UPDATE Students SET Marks = 90 WHERE ID = 1;
Delete Data:
DELETE FROM Students WHERE ID = 1;
Solved Examples
Example 1:
Query to find all employees earning more than 50,000.
SELECT * FROM Employees WHERE Salary > 50000;
Example 2:
Query to count the number of students in the table.
SELECT COUNT(*) FROM Students;
Example 3:
Query to fetch unique department names.
SELECT DISTINCT Department FROM Employees;
Practice Questions (MCQs)
Which SQL keyword is used to remove a table completely?
(a) DROP
(b) DELETE
(c) TRUNCATE
(d) REMOVEWhat does the
DISTINCT
keyword do?
(a) Removes duplicate rows
(b) Deletes null values
(c) Counts rows
(d) Filters columnsWhich command is part of DML?
(a) CREATE
(b) UPDATE
(c) ALTER
(d) DROPWhich SQL clause is used to filter results?
(a) GROUP BY
(b) HAVING
(c) WHERE
(d) ORDER BY
Tips to Master SQL Queries
- Practice basic SELECT queries daily.
- Learn to use JOINs (INNER, LEFT, RIGHT, FULL).
- Revise aggregate functions (SUM, AVG, COUNT, MAX, MIN).
- Focus on subqueries and nested queries.
- Solve previous interview SQL questions.
FAQs:
Q1. What is the difference between DELETE and TRUNCATE?
DELETE removes selected rows, TRUNCATE removes all rows (faster).
Q2. What is a Primary Key in SQL?
A unique identifier for each record in a table.
Q3. What is the difference between WHERE and HAVING?
WHERE filters rows before grouping, HAVING filters groups after aggregation.
Q4. Are SQL queries case-sensitive?
SQL keywords are not case-sensitive, but string values and table names may be.