🗃️ Structured Query Language (SQL) Quiz — Class 12 Computer Science (Code 083)
Practice 20 CBSE Class 12 Computer Science (Code 083) MCQs on SQL — DDL & DML, data types (CHAR, VARCHAR, INT, FLOAT), constraints (NOT NULL, UNIQUE, PRIMARY KEY), SELECT/WHERE/DISTINCT, GROUP BY/HAVING, aggregate functions (MAX, MIN, AVG, SUM, COUNT), LIKE/BETWEEN, and joins (Cartesian product, equi-join, natural join).
Every query below has been tested against a real database. This set includes 1 board-style predict-the-output question with sample table data. Tap any question to reveal the answer with a clear explanation.
🗃️ SQL Basics
What does SQL stand for?
- Standard Query Language
- Simple Query Language
- Structured Query Language
- Structured Query Logic
Show Answer & Explanation
✅ Answer: (c) Structured Query Language
SQL stands for Structured Query Language — the standard language used to create, manage, and query relational databases.
🏗️ DDL
What is the purpose of Data Definition Language (DDL) in SQL?
- To manipulate data
- To retrieve data from the database
- To define and manage the structure of a database
- To perform mathematical operations
Show Answer & Explanation
✅ Answer: (c) To define and manage the structure of a database
DDL commands (CREATE, ALTER, DROP) are used to define and manage the structure of databases and tables — as opposed to DML, which manipulates the actual data inside them.
🔤 Data Types
Which data type is used for storing variable-length character strings in SQL?
- CHAR(n)
- INT
- VARCHAR(n)
- FLOAT
Show Answer & Explanation
✅ Answer: (c) VARCHAR(n)
VARCHAR(n) stores text up to n characters, using only as much space as the actual text needs. CHAR(n), by contrast, always reserves the full n characters, padding shorter values with spaces.
🚧 Constraints
What does the NOT NULL constraint specify in SQL?
- Allows NULL values in the column
- Disallows NULL values in the column
- Enforces unique values in the column
- Creates a primary key
Show Answer & Explanation
✅ Answer: (b) Disallows NULL values in the column
NOT NULL forces a column to always have a value — attempting to insert a row without one for that column will be rejected.
🏗️ DDL
How can you create a new database in SQL?
- CREATE DATABASE database_name;
- USE DATABASE database_name;
- SHOW DATABASES;
- DROP DATABASE database_name;
Show Answer & Explanation
✅ Answer: (a) CREATE DATABASE database_name;
CREATE DATABASE database_name; is the correct command. USE switches to an existing database, SHOW DATABASES lists them, and DROP DATABASE deletes one entirely.
✏️ DML
What is the purpose of the SELECT statement in SQL?
- To insert data into a table
- To update data in a table
- To retrieve data from a table
- To delete data from a table
Show Answer & Explanation
✅ Answer: (c) To retrieve data from a table
SELECT is used purely to retrieve (query) data from one or more tables — it’s the most frequently used SQL statement.
✏️ DML
What is the meaning of the DISTINCT clause in SQL?
- Retrieves all records from a table
- Retrieves unique values from a specified column
- Deletes duplicate records from a table
- Updates existing records in a table
Show Answer & Explanation
✅ Answer: (b) Retrieves unique values from a specified column
DISTINCT filters out repeated values, returning only the unique ones from the specified column(s) — e.g. SELECT DISTINCT Class FROM Students; lists each class name just once, no matter how many students are in it.
✏️ DML
How can you filter data using the WHERE clause in SQL?
- SELECT DISTINCT column_name FROM table_name WHERE condition;
- SELECT * FROM table_name WHERE condition;
- DELETE FROM table_name WHERE condition;
- UPDATE table_name SET column_name = value WHERE condition;
Show Answer & Explanation
✅ Answer: (b) SELECT * FROM table_name WHERE condition;
SELECT * FROM table_name WHERE condition; is the standard pattern to retrieve only the rows matching a given condition. (The other three options also correctly use WHERE, but for DELETE/UPDATE purposes rather than a basic SELECT filter.)
📊 Aggregate Functions
What is the purpose of the GROUP BY clause in SQL?
- To sort data in ascending order
- To group rows that have the same values in specified columns
- To join two tables
- To add a new column to a table
Show Answer & Explanation
✅ Answer: (b) To group rows that have the same values in specified columns
GROUP BY collects rows sharing the same value in a given column into groups — almost always paired with an aggregate function like COUNT(), SUM(), or AVG() to summarize each group.
🔗 Joins
How do you perform an equi-join in SQL, following the standard CBSE-taught approach?
- SELECT * FROM table1, table2 WHERE table1.column = table2.column;
- SELECT * FROM table1 LEFT JOIN table2 ON condition;
- SELECT * FROM table1 RIGHT JOIN table2 ON condition;
- SELECT * FROM table1 FULL OUTER JOIN table2 ON condition;
Show Answer & Explanation
✅ Answer: (a) SELECT * FROM table1, table2 WHERE table1.column = table2.column;
The CBSE Class 12 syllabus specifically teaches equi-join using comma-separated tables in the FROM clause combined with an equality condition in WHERE — e.g. SELECT * FROM table1, table2 WHERE table1.id = table2.id;. (This question is corrected/clarified from a version whose options included LEFT/RIGHT JOIN syntax, which are outer joins not covered in this syllabus and aren’t examples of an equi-join at all — an equi-join specifically means joining on an equality condition, which the comma+WHERE method demonstrates directly.)
✏️ DML
What does the UPDATE command in SQL do?
- Deletes records from a table
- Modifies existing records in a table
- Adds a new table to the database
- Retrieves data from a table
Show Answer & Explanation
✅ Answer: (b) Modifies existing records in a table
UPDATE changes the values of existing rows that match a given condition — e.g. UPDATE Students SET Marks = 90 WHERE RollNo = 1;.
✏️ DML
Which keyword is used for removing records from a table in SQL?
- DELETE
- REMOVE
- DROP
- ERASE
Show Answer & Explanation
✅ Answer: (a) DELETE
DELETE removes rows matching a condition from a table (e.g. DELETE FROM Students WHERE RollNo = 3;). Note that DROP is different — it removes the entire table structure, not just some rows.
📊 Aggregate Functions
What is the purpose of the HAVING clause in SQL?
- To filter individual rows before any grouping happens
- To filter the results of aggregate functions AFTER grouping
- To create an alias for a column
- To group rows based on a condition
Show Answer & Explanation
✅ Answer: (b) To filter the results of aggregate functions AFTER grouping
HAVING filters groups based on aggregate results (like HAVING AVG(Marks) > 85), applied after GROUP BY has run — this is exactly why HAVING can filter on aggregates, whereas WHERE (which runs before grouping) cannot.
📊 Aggregate Functions
What does the MAX() function do in SQL?
- Calculates the average value
- Retrieves the maximum value in a column
- Counts the number of rows
- Retrieves the minimum value in a column
Show Answer & Explanation
✅ Answer: (b) Retrieves the maximum value in a column
MAX() returns the largest value found in the specified column across the matching rows.
🔗 Joins
What is a Cartesian product in SQL?
- The result of combining every row of one table with every row of another, giving rows = (rows in table1) × (rows in table2)
- A type of join that combines rows based on common values only
- The process of creating a new table
- A function to calculate the average
Show Answer & Explanation
✅ Answer: (a) The result of combining every row of one table with every row of another, giving rows = (rows in table1) × (rows in table2)
A Cartesian product (from SELECT * FROM table1, table2; with no WHERE condition) pairs every row of the first table with every row of the second — a 5-row table joined with a 2-row table produces 5 × 2 = 10 resulting rows.
🔤 Data Types
What is the key difference between CHAR(n) and VARCHAR(n) in SQL?
- They are functionally identical in every way
- CHAR(n) always uses exactly n characters of storage (padding shorter values with spaces); VARCHAR(n) uses only as much space as the actual value needs, up to n
- VARCHAR(n) always uses exactly n characters; CHAR(n) is variable-length
- CHAR(n) can only store numbers, not text
Show Answer & Explanation
✅ Answer: (b) CHAR(n) always uses exactly n characters of storage (padding shorter values with spaces); VARCHAR(n) uses only as much space as the actual value needs, up to n
CHAR(n) is fixed-length — a value shorter than n gets padded with trailing spaces to fill it exactly. VARCHAR(n) is variable-length — it only uses as much storage as the actual text requires, up to the maximum n.
🔗 Joins
What is the key difference between a natural join and an equi-join?
- They are exactly the same thing
- A natural join automatically matches columns with the same name across both tables and removes the duplicate column from the result; an equi-join requires an explicit equality condition and keeps both columns
- An equi-join can only be used on two identical tables
- A natural join always returns zero rows
Show Answer & Explanation
✅ Answer: (b) A natural join automatically matches columns with the same name across both tables and removes the duplicate column from the result; an equi-join requires an explicit equality condition and keeps both columns
A natural join automatically finds columns with matching names in both tables, joins on them, and shows that shared column just once in the result. An equi-join requires you to explicitly write the equality condition (e.g. WHERE t1.id = t2.id), and the result keeps both copies of the joined column (from each table).
✏️ DML
Which SQL clause is used for pattern matching in text, such as finding all names starting with a specific letter?
- MATCH
- LIKE
- FIND
- SEARCH
Show Answer & Explanation
✅ Answer: (b) LIKE
LIKE, combined with wildcard characters, does pattern matching — e.g. WHERE Name LIKE 'R%' matches any name starting with “R” (the % matches any sequence of characters).
✏️ DML
Which operator is used to select values within a specified range (inclusive of both ends)?
- IN
- LIKE
- BETWEEN
- RANGE
Show Answer & Explanation
✅ Answer: (c) BETWEEN
BETWEEN low AND high selects all values falling within that range, including both endpoints — e.g. WHERE Marks BETWEEN 80 AND 90 includes marks of exactly 80 and exactly 90 too.
⭐ Board-Style Programs
Given a Students table (RollNo, Name, Class, Marks) with 5 rows — two students in Class ’12A’ with marks 85 and 95, and three students spread across ’12A’ and ’12B’ with other marks — what does the following query return?
SELECT Class, AVG(Marks) FROM Students GROUP BY Class;
- One row per student, showing their individual marks
- One row per class, showing the average marks for each class
- A single row with the overall average of all marks combined
- An error, since AVG() cannot be used with GROUP BY
Show Answer & Explanation
✅ Answer: (b) One row per class, showing the average marks for each class
GROUP BY Class collects all rows into groups by class, and AVG(Marks) computes the average within each group — so the result has exactly one row per distinct class, each showing that class’s average marks.
