Current Affairs updated daily
KalviGavaniKalviGavani
Class 12 · Tamil Nadu State Board

Samacheer Kalvi 12th Computer Science Chapter 12 SQL Exercise Solutions | KalviGavani

Complete TN Board Class 12 Computer Science Chapter 12 Structured Query Language exercise solutions — DDL, DML, DQL, constraints, joins, subqueries with worked SQL examples.

Updated 13 June 202610 min read

Chapter 12 — Structured Query Language (SQL): Exercise Solutions Guide

How to Use This Page

1. Attempt every exercise in your Samacheer Kalvi textbook on your own first. 2. Compare your answers with the solutions below — focus on the method, not just the final result. 3. Practise the SQL statements on a computer using SQLite or MySQL whenever possible. 4. Review the Common Mistakes and Exam Checklist before your exam.


Key Concepts

CategoryCommandsPurpose
DDLCREATE, ALTER, DROP, TRUNCATEDefine or modify table structure
DMLINSERT, UPDATE, DELETEManipulate data in tables
DQLSELECTRetrieve / query data
DCLGRANT, REVOKEControl access permissions
TCLCOMMIT, ROLLBACK, SAVEPOINTManage transactions

Constraints ensure data integrity: PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, CHECK, DEFAULT.

Aggregate Functions: COUNT(), SUM(), AVG(), MAX(), MIN().

Clauses: WHERE (row filter), GROUP BY (grouping), HAVING (group filter), ORDER BY (sorting), DISTINCT (unique values).


Multiple Choice Questions

Q1. Which of the following is a DDL command?

  • (a) INSERT   (b) SELECT   (c) ALTER   (d) UPDATE
Answer(c) ALTER — DDL commands define structure; ALTER modifies a table's structure.

Q2. The SQL keyword used to eliminate duplicate rows is:

  • (a) UNIQUE   (b) DISTINCT   (c) DIFFERENT   (d) REMOVE
Answer(b) DISTINCT — SELECT DISTINCT column FROM table returns only unique values.

Q3. Which clause is used to filter groups after aggregation?

  • (a) WHERE   (b) GROUP BY   (c) HAVING   (d) ORDER BY
Answer(c) HAVING — HAVING works on groups created by GROUP BY, unlike WHERE which works on individual rows.

Q4. A PRIMARY KEY constraint implies:

  • (a) UNIQUE only   (b) NOT NULL only   (c) UNIQUE + NOT NULL   (d) CHECK
Answer(c) UNIQUE + NOT NULL — a primary key must have a unique, non-null value for every row.

Q5. Which command removes all rows from a table but keeps its structure?

  • (a) DROP   (b) DELETE   (c) TRUNCATE   (d) ALTER
Answer(c) TRUNCATE — removes all data quickly; the table definition remains. DROP removes the entire table.

Q6. The foreign key references:

  • (a) the same table   (b) another table's primary key   (c) any column   (d) an index
Answer(b) another table's primary key — a foreign key creates a referential link to a primary key in another (or the same) table.

Fill in the Blanks

1. The ______ clause sorts the result set in ascending or descending order. → ORDER BY 2. ______ function returns the number of rows in a result set. → COUNT() 3. The command to add a new column to an existing table is ______. → ALTER TABLE ... ADD COLUMN 4. To insert a row into a table, the command used is ______. → INSERT INTO 5. ______ constraint ensures that a column cannot contain null values. → NOT NULL 6. The ______ operation combines rows from two tables based on a related column. → JOIN


True or False

1. DROP TABLE removes both data and structure of a table.
AnswerTrue 2. The WHERE clause can use aggregate functions. → False — WHERE filters rows before grouping; use HAVING for aggregates. 3. DISTINCT applies to all columns listed in the SELECT clause. → True 4. A table can have only one primary key. → True 5. The DEFAULT constraint inserts a system date automatically. → False — DEFAULT inserts the specified default value, not necessarily a date. 6. ORDER BY sorts results in descending order by default. → False — default is ascending (ASC).

Match the Following

Column AColumn B
1. CREATEa. Remove all rows, keep structure
2. DROPb. Define a new table
3. TRUNCATEc. Remove entire table
4. INSERTd. Add a row of data
5. HAVINGe. Filter groups after aggregation
Answer1-b, 2-c, 3-a, 4-d, 5-e

Short Answer Questions

Q1. Differentiate between DDL and DML with two examples each.

DDLDML
Defines/modifies table structureManipulates data within tables
CREATE TABLE, ALTER TABLE, DROP TABLEINSERT, UPDATE, DELETE
Auto-commits (in most DBMS)May require explicit COMMIT

Q2. What is a constraint? List any four constraints with their purpose.

A constraint is a rule enforced on table columns to maintain data accuracy and integrity.

ConstraintPurpose
PRIMARY KEYUniquely identifies each row; no nulls allowed
FOREIGN KEYEnsures referential integrity with another table
NOT NULLColumn must have a value (cannot be empty)
CHECKValues must satisfy a specified condition

Q3. Explain the difference between WHERE and HAVING clauses.

  • WHERE filters individual rows before grouping; cannot use aggregate functions.
  • HAVING filters groups after GROUP BY aggregation; can use aggregate functions.
-- WHERE filters rows
SELECT dept, AVG(salary) FROM emp WHERE salary > 10000 GROUP BY dept;

-- HAVING filters groups
SELECT dept, AVG(salary) FROM emp GROUP BY dept HAVING AVG(salary) > 30000;

Q4. Write the syntax to create a table with constraints.

CREATE TABLE student (
    roll_no   INT PRIMARY KEY,
    name      VARCHAR(50) NOT NULL,
    dept      VARCHAR(30) DEFAULT 'CS',
    mark      INT CHECK (mark >= 0 AND mark <= 100),
    course_id INT,
    FOREIGN KEY (course_id) REFERENCES course(course_id)
);

Q5. What is the difference between DELETE and TRUNCATE?

DELETETRUNCATE
DML commandDDL command
Can use WHERE to delete specific rowsRemoves all rows; no WHERE clause
Slower (logs each row deletion)Faster (deallocates data pages)
Can be rolled backCannot be rolled back (in most DBMS)

Detailed Answer Questions

Q1. Explain DDL commands (CREATE, ALTER, DROP) with examples.

CREATE — defines a new table (or database, view, index):

CREATE TABLE employee (
    emp_id    INT PRIMARY KEY,
    emp_name  VARCHAR(50) NOT NULL,
    salary    DECIMAL(10,2),
    dept      VARCHAR(30)
);

ALTER — modifies an existing table's structure:

-- Add a new column
ALTER TABLE employee ADD COLUMN joining_date DATE;

-- Modify a column's data type
ALTER TABLE employee MODIFY COLUMN salary DECIMAL(12,2);

-- Drop a column
ALTER TABLE employee DROP COLUMN dept;

DROP — removes the entire table (structure + data):

DROP TABLE employee;

Once dropped, the table cannot be recovered (unless a backup exists).

Q2. Explain DML commands (INSERT, UPDATE, DELETE) with examples.

INSERT — adds new rows to a table:

-- Insert a full row
INSERT INTO employee VALUES (101, 'Arun', 55000.00, 'IT');

-- Insert specific columns
INSERT INTO employee (emp_id, emp_name) VALUES (102, 'Bala');

UPDATE — modifies existing data:

-- Update specific rows
UPDATE employee SET salary = 60000 WHERE emp_id = 101;

-- Update all rows (dangerous without WHERE!)
UPDATE employee SET dept = 'HR';

DELETE — removes rows:

-- Delete specific rows
DELETE FROM employee WHERE emp_id = 102;

-- Delete all rows (table structure remains)
DELETE FROM employee;

Q3. Explain SELECT queries with WHERE, ORDER BY, GROUP BY, and aggregate functions.

Basic SELECT:

SELECT emp_name, salary FROM employee;
SELECT * FROM employee;

WHERE clause — row-level filtering:

SELECT emp_name, salary FROM employee WHERE dept = 'IT';
SELECT * FROM employee WHERE salary BETWEEN 40000 AND 70000;
SELECT * FROM employee WHERE emp_name LIKE 'A%';

ORDER BY — sorting results:

SELECT * FROM employee ORDER BY salary DESC;
SELECT * FROM employee ORDER BY dept ASC, salary DESC;

Aggregate Functions:

SELECT COUNT(*) FROM employee;
SELECT AVG(salary) FROM employee WHERE dept = 'IT';
SELECT MAX(salary), MIN(salary) FROM employee;
SELECT SUM(salary) FROM employee;

GROUP BY — grouping rows:

SELECT dept, COUNT(*) FROM employee GROUP BY dept;
SELECT dept, AVG(salary) FROM employee GROUP BY dept;

HAVING — filtering groups:

SELECT dept, AVG(salary)
FROM employee
GROUP BY dept
HAVING AVG(salary) > 50000;

DISTINCT — removing duplicates:

SELECT DISTINCT dept FROM employee;

Q4. Explain SQL Joins with examples.

Joins combine rows from two or more tables based on a related column.

-- Sample tables
-- employee: emp_id, emp_name, dept_id
-- department: dept_id, dept_name

INNER JOIN — returns only matching rows from both tables:

SELECT e.emp_name, d.dept_name
FROM employee e
INNER JOIN department d ON e.dept_id = d.dept_id;

LEFT JOIN — all rows from the left table, matched rows from the right (NULL if no match):

SELECT e.emp_name, d.dept_name
FROM employee e
LEFT JOIN department d ON e.dept_id = d.dept_id;

RIGHT JOIN — all rows from the right table, matched rows from the left:

SELECT e.emp_name, d.dept_name
FROM employee e
RIGHT JOIN department d ON e.dept_id = d.dept_id;

Q5. Explain subqueries with examples.

A subquery is a query nested inside another query.

Subquery in WHERE clause:

-- Employees earning more than the average salary
SELECT emp_name, salary
FROM employee
WHERE salary > (SELECT AVG(salary) FROM employee);

Subquery with IN:

-- Employees in IT or HR department
SELECT emp_name
FROM employee
WHERE dept_id IN (
    SELECT dept_id FROM department WHERE dept_name IN ('IT', 'HR')
);

Subquery in FROM clause (derived table):

SELECT dept, avg_sal
FROM (
    SELECT dept, AVG(salary) AS avg_sal
    FROM employee
    GROUP BY dept
) AS dept_avg
WHERE avg_sal > 50000;

Common Mistakes

MistakeWhy It's WrongFix
UPDATE employee SET salary = 60000; without WHEREUpdates every row in the tableAlways include a WHERE clause unless you truly want all rows
DELETE FROM employee; without WHEREDeletes all rowsAdd WHERE to target specific rows
Using WHERE salary > AVG(salary)Aggregate functions cannot appear in WHEREUse a subquery: WHERE salary > (SELECT AVG(salary) FROM employee)
DROP TABLE when you mean TRUNCATEDROP removes the table entirely; TRUNCATE only removes dataUse TRUNCATE if you want to keep the table structure
Forgetting COMMIT after DMLChanges are not saved permanentlyCall COMMIT (or enable auto-commit) after INSERT/UPDATE/DELETE
Confusing HAVING with WHEREWHERE filters rows, HAVING filters groupsUse HAVING only with GROUP BY and aggregate functions
Omitting ON clause in JOINsProduces a cross join (Cartesian product)Always specify the join condition with ON

Exam Checklist

  • [ ] Can write CREATE TABLE with all six constraints (PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, CHECK, DEFAULT)
  • [ ] Can differentiate DDL, DML, DQL, DCL, TCL with examples
  • [ ] Can write INSERT, UPDATE, DELETE with and without WHERE
  • [ ] Can write SELECT with WHERE, ORDER BY, GROUP BY, HAVING, DISTINCT
  • [ ] Can use all five aggregate functions (COUNT, SUM, AVG, MAX, MIN)
  • [ ] Can write INNER JOIN and LEFT JOIN queries
  • [ ] Can write subqueries in WHERE and FROM clauses
  • [ ] Know the difference between DELETE and TRUNCATE
  • [ ] Know the difference between WHERE and HAVING
  • [ ] Can explain PRIMARY KEY vs FOREIGN KEY vs UNIQUE constraints

Frequently Asked Questions

DDL (Data Definition Language) defines or modifies the structure of tables — CREATE, ALTER, DROP, TRUNCATE. DML (Data Manipulation Language) works on the data inside tables — INSERT, UPDATE, DELETE. DQL (Data Query Language) retrieves data using SELECT.
← Browse all Class 12 Exercise Solutions

Other question types — same chapter