0% found this document useful (0 votes)
15 views13 pages

SQL Practice Questions (All Topics)

The document is a SQL command practice worksheet that includes instructions for creating tables, altering table structures, dropping tables, and performing various data manipulation tasks such as inserting, updating, and deleting records. It also covers SQL queries for selecting data with conditions, grouping results, and utilizing functions for single and multi-row operations. The document serves as a comprehensive guide for practicing SQL commands and understanding database management concepts.

Uploaded by

tharunsundaar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views13 pages

SQL Practice Questions (All Topics)

The document is a SQL command practice worksheet that includes instructions for creating tables, altering table structures, dropping tables, and performing various data manipulation tasks such as inserting, updating, and deleting records. It also covers SQL queries for selecting data with conditions, grouping results, and utilizing functions for single and multi-row operations. The document serves as a comprehensive guide for practicing SQL commands and understanding database management concepts.

Uploaded by

tharunsundaar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SQL COMMAND PRACTICE WORKSHEET

Q1. Write the SQL command to create a table STUDENT with columns:
AdmNo INT PRIMARY KEY,
Name VARCHAR(40),
Class CHAR(2),
Section CHAR(1),
Marks INT

Q2. Create the table EMPLOYEE with columns:


EmpID INT PRIMARY KEY,
EmpName VARCHAR(30),
Department VARCHAR(20),
Salary DECIMAL(10,2),
HireDate DATE

Q3. Create table BOOKS:


BookID CHAR(5) PRIMARY KEY,
Title VARCHAR(50),
Author VARCHAR(30),
Price DECIMAL(7,2),
Publisher VARCHAR(40)

Q4. Create table SUPPLIERS:


SuppID CHAR(5) PRIMARY KEY,
SuppName VARCHAR(50),
City VARCHAR(30),
ContactNo VARCHAR(15)

Q5. Create table TRANSACTION:


TransID INT PRIMARY KEY,
CustID INT,
TransDate DATE,
Amount DECIMAL(8,2),
PayMode VARCHAR(10)

ALTER TABLE – ADD/MODIFY/DROP Columns (5 Questions with Datatype

Q1. Add a column Email VARCHAR(50) to EMPLOYEE table.


ALTER TABLE EMPLOYEE ADD Email VARCHAR(50);

Q2. Modify the Salary column datatype to DECIMAL(12,2) in EMPLOYEE table.


ALTER TABLE EMPLOYEE MODIFY Salary DECIMAL(12,2);
Q3. Add column Category CHAR(10) to BOOKS table.
ALTER TABLE BOOKS ADD Category CHAR(10);

Q4. Drop the column ContactNo from SUPPLIERS table.


ALTER TABLE SUPPLIERS DROP COLUMN ContactNo;

Q5. Modify Name column size in STUDENT to VARCHAR(60).


ALTER TABLE STUDENT MODIFY Name VARCHAR(60);

DROP TABLE Command

Q1. Drop the STUDENT table.


DROP TABLE STUDENT;

Q2. Drop the TRANSACTION table.


DROP TABLE TRANSACTION;

Q3. Drop the SUPPLIERS table.


DROP TABLE SUPPLIERS;

Q4. Drop the EMPLOYEE table.


DROP TABLE EMPLOYEE;

Q5. Drop the BOOKS table.


DROP TABLE BOOKS;

DISPLAY TABLE STRUCTURE (DESC/DESCRIBE)

Q1. Display the structure of STUDENT table.


DESC STUDENT;

Q2. Show the design of EMPLOYEE table.


DESC EMPLOYEE;

Q3. Display the structure of BOOKS table.


DESC BOOKS;

Q4. Use DESCRIBE on TRANSACTION table.


DESCRIBE TRANSACTION;

Q5. Show all column names/types of SUPPLIERS.


DESC SUPPLIERS;

DML – INSERT

Q1. Insert the record (1001, 'Raj', 'XII', 'A', 85) into STUDENT.

Q2. Add new employee ('E101', 'Arun', 'HR', 45000, '2021-09-21') to EMPLOYEE.

Q3. Insert into BOOKS: ('B01', 'Python Programming', 'R Singh', 550.00, 'Oxford').

Q4. Insert supplier (S01, 'TechTrade', 'Delhi', '9823456765').

Q5. Insert multiple rows into TRANSACTION:


(2, 101, '2022-06-18', 4500.00, 'Cash');

DML COMMANDS

Q1. Update the Salary to 50000 for employee 'Arun'.


Q2. Update Marks by adding 5 for students in Section 'A'.
Q3. Change the City to 'Mumbai' for supplier 'TechTrade'.
Q4. Update Price = Price + 50 for all books by 'R Singh'.
Q5. Update PayMode to 'UPI' where Amount > 5000.

DML COMMANDS – DELETE


Q1. Delete all employees from HR department.
Q2. Delete records from BOOKS where Price < 300.
Q3. Delete entries from TRANSACTION where PayMode = 'Cash'.
Q4. Delete student record where AdmNo = 1001.
Q5. Delete all suppliers located in 'Delhi'.

SELECT WITH DISTINCT AND WHERE (15 Questions – All Condition Types)

Q1. Display distinct Department names from EMPLOYEE table.


Q2. Display Name and Marks of students where Marks > 80.
Q3. Display all books where Price BETWEEN 300 AND 700.
Q4. Show all employees where Department IN ('HR','Sales').
Q5. Display suppliers whose City NOT IN ('Delhi','Kolkata').
Q6. Show transactions where Amount > 1000 AND PayMode = 'UPI'.
Q7. Display students where Class = 'XII' OR Marks < 40.
Q8. Retrieve all employees with Salary <= 40000.
Q9. Show all books where Title LIKE '%Python%'.
Q10. Display students where Name LIKE 'R%'.
Q11. Show all records where TransDate IS NULL.
Q12. Display employees where Department IS NOT NULL.
Q13. Show books where Author LIKE '_a%' (second letter ‘a’).
Q14. Retrieve all transactions where Amount BETWEEN 1000 AND 2000.
Q15. Display unique cities from SUPPLIERS table.

GROUP BY

Q1. Display average salary department-wise from EMPLOYEE.


Q2. Show total marks section-wise from STUDENT.
Q3. Display total sales per customer from TRANSACTION.
Q4. Show count of books by each Author.
Q5. Display number of suppliers per city.

GROUP BY WITH HAVING

Q1. Display departments having average salary more than 40000.


Q2. Show sections having more than 5 students.
Q3. Display authors having more than 2 books in BOOKS table.
Q4. Show each City having total suppliers greater than 3.
Q5. Display customers with total transaction amount > 5000.
Q6. Show each department with employee count > 3.
Q7. Display classes having average marks above 70.
Q8. Show authors where total book price sum > 2000.
Q9. Display PayMode having total number of transactions > 5.
Q10. Show cities grouped by supplier count, only where count > 2.

SINGLE ROW FUNCTIONS

Q1. Display UPPER(Name) from STUDENT.


Q2. Show LOWER(Department) from EMPLOYEE.
Q3. Display SUBSTRING(Title,1,5) from BOOKS.
Q4. Show LENGTH(Name) from STUDENT.
Q5. Display ROUND(Salary,0) for all employees.
Q6. Display DAY(HireDate) and MONTH(HireDate) from EMPLOYEE.
Q7. Show CONCAT(Name, '-', Section) from STUDENT.
Q8. Display MOD(Marks,10) from STUDENT.
Q9. Display REPLACE(Title,'Python','SQL') from BOOKS.
Q10. Show current date using CURDATE()

12. MULTI ROW (GROUP) FUNCTIONS

Q1. Display total number of students using COUNT(*).


Q2. Show maximum Salary from EMPLOYEE.
Q3. Display minimum Price from BOOKS.
Q4. Show average Marks from STUDENT.
Q5. Display total Amount from TRANSACTION.
Q6. Find average Salary department-wise.
Q7. Show maximum transaction amount customer-wise.
Q8. Display total suppliers and city count.
Q9. Retrieve number of books by each publisher.
Q10. Show total sum of marks for section 'A'.

SQL Worksheet – Answer Key

[Link] – CREATE TABLE (with Datatypes)

-- Q1
CREATE TABLE STUDENT (
AdmNo INT PRIMARY KEY,
Name VARCHAR(40),
Class CHAR(2),
Section CHAR(1),
Marks INT
);

-- Q2
CREATE TABLE EMPLOYEE (
EmpID INT PRIMARY KEY,
EmpName VARCHAR(30),
Department VARCHAR(20),
Salary DECIMAL(10,2),
HireDate DATE
);

-- Q3
CREATE TABLE BOOKS (
BookID CHAR(5) PRIMARY KEY,
Title VARCHAR(50),
Author VARCHAR(30),
Price DECIMAL(7,2),
Publisher VARCHAR(40)
);

-- Q4
CREATE TABLE SUPPLIERS (
SuppID CHAR(5) PRIMARY KEY,
SuppName VARCHAR(50),
City VARCHAR(30),
ContactNo VARCHAR(15)
);

-- Q5
CREATE TABLE TRANSACTION (
TransID INT PRIMARY KEY,
CustID INT,
TransDate DATE,
Amount DECIMAL(8,2),
PayMode VARCHAR(10)
);

[Link] TABLE (Add/Modify/Drop columns)

-- Q1
ALTER TABLE EMPLOYEE ADD Email VARCHAR(50);

-- Q2
ALTER TABLE EMPLOYEE MODIFY Salary DECIMAL(12,2);

-- Q3
ALTER TABLE BOOKS ADD Category CHAR(10);

-- Q4
ALTER TABLE SUPPLIERS DROP COLUMN ContactNo;

-- Q5
ALTER TABLE STUDENT MODIFY Name VARCHAR(60);

[Link] TABLE

-- Q1
DROP TABLE STUDENT;
-- Q2
DROP TABLE TRANSACTION;

-- Q3
DROP TABLE SUPPLIERS;

-- Q4
DROP TABLE EMPLOYEE;

-- Q5
DROP TABLE BOOKS;

[Link] TABLE STRUCTURE (DESC/DESCRIBE)

-- Q1
DESC STUDENT;

-- Q2
DESC EMPLOYEE;

-- Q3
DESC BOOKS;

-- Q4
DESCRIBE TRANSACTION;

-- Q5
DESC SUPPLIERS;
```

[Link] – INSERT

-- Q1
INSERT INTO STUDENT (AdmNo, Name, Class, Section, Marks)
VALUES (1001, 'Raj', 'XII', 'A', 85);

-- Q2
INSERT INTO EMPLOYEE (EmpID, EmpName, Department, Salary, HireDate)
VALUES (101, 'Arun', 'HR', 45000.00, '2021-09-21');
-- Q3
INSERT INTO BOOKS (BookID, Title, Author, Price, Publisher)
VALUES ('B01', 'Python Programming', 'R Singh', 550.00, 'Oxford');

-- Q4
INSERT INTO SUPPLIERS (SuppID, SuppName, City, ContactNo)
VALUES ('S01', 'TechTrade', 'Delhi', '9823456765');

-- Q5 (Two rows shown; can add more)


INSERT INTO TRANSACTION (TransID, CustID, TransDate, Amount, PayMode)
VALUES (2, 101, '2022-06-18', 4500.00, 'Cash');

[Link] – UPDATE

-- Q1
UPDATE EMPLOYEE SET Salary = 50000.00 WHERE EmpName = 'Arun';

-- Q2
UPDATE STUDENT SET Marks = Marks + 5 WHERE Section = 'A';

-- Q3
UPDATE SUPPLIERS SET City = 'Mumbai' WHERE SuppName = 'TechTrade';

-- Q4
UPDATE BOOKS SET Price = Price + 50 WHERE Author = 'R Singh';

-- Q5
UPDATE TRANSACTION SET PayMode = 'UPI' WHERE Amount > 5000;

[Link] – DELETE

-- Q1
DELETE FROM EMPLOYEE WHERE Department = 'HR';

-- Q2
DELETE FROM BOOKS WHERE Price < 300;

-- Q3
DELETE FROM TRANSACTION WHERE PayMode = 'Cash';
-- Q4
DELETE FROM STUDENT WHERE AdmNo = 1001;

-- Q5
DELETE FROM SUPPLIERS WHERE City = 'Delhi';

[Link] WITH DISTINCT AND WHERE (Conditions, all Syntax)

-- Q1
SELECT DISTINCT Department FROM EMPLOYEE;

-- Q2
SELECT Name, Marks FROM STUDENT WHERE Marks > 80;

-- Q3
SELECT * FROM BOOKS WHERE Price BETWEEN 300 AND 700;

-- Q4
SELECT * FROM EMPLOYEE WHERE Department IN ('HR','Sales');

-- Q5
SELECT * FROM SUPPLIERS WHERE City NOT IN ('Delhi','Kolkata');

-- Q6
SELECT * FROM TRANSACTION WHERE Amount > 1000 AND PayMode = 'UPI';

-- Q7
SELECT * FROM STUDENT WHERE Class = 'XII' OR Marks < 40;

-- Q8
SELECT * FROM EMPLOYEE WHERE Salary <= 40000;

-- Q9
SELECT * FROM BOOKS WHERE Title LIKE '%Python%';

-- Q10
SELECT * FROM STUDENT WHERE Name LIKE 'R%';

-- Q11
SELECT * FROM TRANSACTION WHERE TransDate IS NULL;

-- Q12
SELECT * FROM EMPLOYEE WHERE Department IS NOT NULL;
-- Q13
SELECT * FROM BOOKS WHERE Author LIKE '_a%';

-- Q14
SELECT * FROM TRANSACTION WHERE Amount BETWEEN 1000 AND 2000;

-- Q15
SELECT DISTINCT City FROM SUPPLIERS;

9. GROUP BY

-- Q1
SELECT Department, AVG(Salary) FROM EMPLOYEE GROUP BY Department;

-- Q2
SELECT Section, SUM(Marks) FROM STUDENT GROUP BY Section;

-- Q3
SELECT CustID, SUM(Amount) FROM TRANSACTION GROUP BY CustID;

-- Q4
SELECT Author, COUNT(*) FROM BOOKS GROUP BY Author;

-- Q5
SELECT City, COUNT(*) FROM SUPPLIERS GROUP BY City;

10. GROUP BY WITH HAVING

-- Q1
SELECT Department, AVG(Salary) FROM EMPLOYEE GROUP BY Department HAVING
AVG(Salary) > 40000;

-- Q2
SELECT Section, COUNT(*) FROM STUDENT GROUP BY Section HAVING COUNT(*) > 5;

-- Q3
SELECT Author, COUNT(*) FROM BOOKS GROUP BY Author HAVING COUNT(*) > 2;

-- Q4
SELECT City, COUNT(*) FROM SUPPLIERS GROUP BY City HAVING COUNT(*) > 3;

-- Q5
SELECT CustID, SUM(Amount) FROM TRANSACTION GROUP BY CustID HAVING
SUM(Amount) > 5000;

-- Q6
SELECT Department, COUNT(*) FROM EMPLOYEE GROUP BY Department HAVING
COUNT(*) > 3;

-- Q7
SELECT Class, AVG(Marks) FROM STUDENT GROUP BY Class HAVING AVG(Marks) > 70;

-- Q8
SELECT Author, SUM(Price) FROM BOOKS GROUP BY Author HAVING SUM(Price) > 2000;

-- Q9
SELECT PayMode, COUNT(*) FROM TRANSACTION GROUP BY PayMode HAVING
COUNT(*) > 5;

-- Q10
SELECT City, COUNT(*) FROM SUPPLIERS GROUP BY City HAVING COUNT(*) > 2;

11. SINGLE ROW FUNCTIONS

-- Q1
SELECT UPPER(Name) FROM STUDENT;

-- Q2
SELECT LOWER(Department) FROM EMPLOYEE;

-- Q3
SELECT SUBSTRING(Title, 1, 5) FROM BOOKS;

-- Q4
SELECT LENGTH(Name) FROM STUDENT;

-- Q5
SELECT ROUND(Salary, 0) FROM EMPLOYEE;

-- Q6
SELECT DAY(HireDate), MONTH(HireDate) FROM EMPLOYEE;
-- Q7
SELECT CONCAT(Name, '-', Section) FROM STUDENT;

-- Q8
SELECT MOD(Marks, 10) FROM STUDENT;

-- Q9
SELECT REPLACE(Title, 'Python', 'SQL') FROM BOOKS;

-- Q10
SELECT CURDATE();

[Link] ROW (GROUP) FUNCTIONS

-- Q1
SELECT COUNT(*) FROM STUDENT;

-- Q2
SELECT MAX(Salary) FROM EMPLOYEE;

-- Q3
SELECT MIN(Price) FROM BOOKS;

-- Q4
SELECT AVG(Marks) FROM STUDENT;

-- Q5
SELECT SUM(Amount) FROM TRANSACTION;

-- Q6
SELECT Department, AVG(Salary) FROM EMPLOYEE GROUP BY Department;

-- Q7
SELECT CustID, MAX(Amount) FROM TRANSACTION GROUP BY CustID;

-- Q8
SELECT COUNT(*) AS TotalSuppliers, COUNT(DISTINCT City) AS CityCount FROM
SUPPLIERS;

-- Q9
SELECT Publisher, COUNT(*) FROM BOOKS GROUP BY Publisher;
-- Q10
SELECT SUM(Marks) FROM STUDENT WHERE Section = 'A';

You might also like