SQL Queries: Complete Guide for DBMS Preparation (2025)

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

  1. DDL (Data Definition Language)

    • CREATE, ALTER, DROP, TRUNCATE

    • Defines and modifies database schema.

  2. DML (Data Manipulation Language)

    • INSERT, UPDATE, DELETE

    • Used for data modification.

  3. DQL (Data Query Language)

    • SELECT

    • Used to retrieve data from tables.

  4. DCL (Data Control Language)

    • GRANT, REVOKE

    • Manages database permissions.

  5. 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)

  1. Which SQL keyword is used to remove a table completely?
    (a) DROP
    (b) DELETE
    (c) TRUNCATE
    (d) REMOVE

  2. What does the DISTINCT keyword do?
    (a) Removes duplicate rows
    (b) Deletes null values
    (c) Counts rows
    (d) Filters columns

  3. Which command is part of DML?
    (a) CREATE
    (b) UPDATE
    (c) ALTER
    (d) DROP

  4. Which 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.

Leave a Comment

Your email address will not be published. Required fields are marked *