Chapter9 Qa Removed
Chapter9 Qa Removed
R
2. Which of the following is not a type of RDBMS?
L
a) MySQL
b) Oracle
N
c) MongoDB
EE
d) PostgreSQL
Answer: c
d) Android
Answer: b
ED
4. Which of the following is a correct SQL statement to create a database named Library?
a) MAKE DATABASE Library;
M
d) DATABASE Library;
Answer: c
A
a) LIST DATABASES;
O
d) SHOW DATABASES;
Answer: d
d) Exit SQL
Answer: c
R
Answer: d
L
a) Fixed length
N
b) Variable length
c) Integer
EE
d) Binary type
Answer: a
H
9. Which constraint ensures uniqueness of values in a column?
AT
a) PRIMARY KEY
b) FOREIGN KEY
M
c) UNIQUE
d) DEFAULT
Answer: c
ED
Answer: b
H
d) ALTER
Answer: c
R
Answer: b
L
a) Foreign key
N
b) Alternate key
c) Composite key
EE
d) Candidate key
Answer: c
H
15. What happens if you run an UPDATE statement without a WHERE clause?
AT
a) Nothing happens
b) Syntax error
M
b) INSERT INTO
c) PUT INTO
M
d) ENTER INTO
A
Answer: b
H
b) MM-DD-YYYY
M
c) YYYY-MM-DD
d) Any format
Answer: c
d) ALL
Answer: a
R
Answer: c
L
a) EXP()
N
b) ROUND()
c) POW()
EE
d) SQRT()
Answer: c
H
21. Which clause is used to sort records in ascending or descending order?
AT
a) GROUP BY
b) ORDER BY
M
c) SORT BY
d) ARRANGE BY
Answer: b
ED
b) DOWN
c) DESC
M
d) LOWER
A
Answer: c
H
23. Which function returns the name of the day from a date?
a) DAYNAME()
O
b) DAY()
M
c) DATENAME()
d) DAYOFWEEK()
Answer: a
24. Which clause helps in fetching only unique values from a column?
a) ONLY
b) UNIQUE
c) DISTINCT
d) NON-REPEATED
Answer: c
R
Answer: b
L
a) CURRENT()
N
b) NOW()
c) TIME()
EE
d) SYSDATE()
Answer: b
H
27. Which of the following is used for pattern matching in SQL?
AT
a) LIKE
b) IN
M
c) AND
d) IS
Answer: a
ED
28. Which symbol is used to match any number of characters in SQL pattern matching?
a) _
M
b) %
c) *
M
d) #
A
Answer: b
H
b) LTRIM()
M
c) TRIM()
d) REMOVE()
Answer: b
d) CAP()
Answer: b
31. Which clause is used to group rows that have the same values?
a) ORDER BY
b) GROUP BY
c) SET BY
d) UNION
R
Answer: b
L
a) LEN()
N
b) LENGTH()
c) SIZE()
EE
d) COUNT()
Answer: b
c) FILTER
d) SELECT
Answer: a
ED
b) HAVING
c) GROUP BY
M
d) FILTER
A
Answer: b
H
b) TOP()
M
c) LAST()
d) HIGH()
Answer: a
36. Which clause in SQL is used to group records that have the same values in specified columns?
a) ORDER BY
b) GROUP BY
c) WHERE
d) HAVING
Answer: b
R
Answer: c
L
a) 5
N
b) 0
c) NULL
EE
d) Error
Answer: c
c) SLICE()
d) CUT()
Answer: a
ED
b) 123.5
c) 123.45
M
d) 123
A
Answer: b
H
b) DELETE
M
c) REMOVE
d) DROP
Answer: d
d) 0
Answer: a
R
Answer: b
L
a) IN
N
b) RANGE
c) BETWEEN
EE
d) BOUND
Answer: c
c) AVG()
d) LCASE()
Answer: c
ED
b) MONTH()
c) GETMONTH()
M
d) MTH()
A
Answer: b
H
47. Which of the following is a valid constraint that can be applied to enforce referential integrity
between two tables?
O
a) DEFAULT
M
b) PRIMARY KEY
c) UNIQUE
d) FOREIGN KEY
Answer: d
48. Which of the following conditions must be satisfied for a FOREIGN KEY to be added to a
table?
a) The table must have a DATE column
49. Which SQL operator is used to check whether a column value falls between two values (inclu-
sive)?
a) IN
R
b) LIKE
c) RANGE
L
d) BETWEEN
Answer: d
N
50. Which keyword is used to delete a particular row in SQL?
EE
a) REMOVE
b) ERASE
c) DELETE
H
AT
d) DROP
Answer: c
M
51. Assertion (A): SQL is used only for retrieving data from databases. Reason (R): SQL stands
for Structured Query Language.
a) Both A and R are true, and R is the correct explanation of A
ED
52. Assertion (A): VARCHAR stores variable-length character strings. Reason (R): CHAR and
A
Answer: c
53. Assertion (A): PRIMARY KEY allows duplicate values in a column. Reason (R): PRIMARY
KEY is used to uniquely identify each record.
a) Both A and R are true, and R is the correct explanation of A
b) Both A and R are true, but R is not the correct explanation of A
c) A is false, but R is true
54. Assertion (A): ALTER TABLE statement can be used to add a new column. Reason (R): ALTER
TABLE can modify the structure of an existing table.
a) Both A and R are true, and R is the correct explanation of A
b) Both A and R are true, but R is not the correct explanation of A
c) A is false, but R is true
R
d) A is true, but R is false
Answer: a
L
55. Assertion (A): The keyword DESC is used to sort records in descending order. Reason (R): The
N
ORDER BY clause displays records in random order by default.
a) Both A and R are true, and R is the correct explanation of A
EE
b) Both A and R are true, but R is not the correct explanation of A
c) A is true, but R is false
d) A is false, but R is true
H
AT
Answer: c
56. Assertion (A): SQL is a case-insensitive language. Reason (R): In SQL, ‘salary’ and ‘SALARY’
M
Answer: a
57. Assertion (A): The CREATE TABLE command is used to define the structure of a new table.
M
Reason (R): CREATE TABLE can define attribute names, data types, and constraints.
A
58. Assertion (A): DEFAULT constraint ensures that every column must have a unique value.
Reason (R): DEFAULT sets a predefined value for a column when no value is provided.
a) Both A and R are true, and R is the correct explanation of A
b) Both A and R are true, but R is not the correct explanation of A
c) A is false, but R is true
d) A is true, but R is false
Answer: c
59. Assertion (A): DELETE statement removes a table from the database. Reason (R): DROP
TABLE statement is used to remove a table permanently.
a) Both A and R are true, and R is the correct explanation of A
b) Both A and R are true, but R is not the correct explanation of A
c) A is false, but R is true
d) A is true, but R is false
R
Answer: c
60. Assertion (A): The WHERE clause in SQL is used to filter groups formed by GROUP BY.
L
Reason (R): HAVING clause filters records before they are grouped.
N
a) Both A and R are true, and R is the correct explanation of A
b) Both A and R are true, but R is not the correct explanation of A
EE
c) A is false, but R is true
d) Both A and R are false
Answer: d
H
AT
61. Assertion (A): INSERT INTO command adds new records into a table. Reason (R): Records
can be inserted without specifying column names if all values are provided in correct order.
M
62. Assertion (A): A FOREIGN KEY can reference a column that is not a primary key. Reason
(R): A foreign key ensures that each value exists in another table’s primary key column.
M
Answer: c
M
63. Assertion (A): ROUND() is used to round numbers to specified decimal places. Reason (R):
ROUND(283.2, 0) will return 283.2.
a) Both A and R are true, and R is the correct explanation of A
b) Both A and R are true, but R is not the correct explanation of A
c) A is true, but R is false
d) A is false, but R is true
Answer: c
64. Assertion (A): The % symbol in SQL is used to match zero or more characters. Reason (R):
The LIKE clause in SQL supports pattern matching using wildcards.
a) Both A and R are true, and R is the correct explanation of A
b) Both A and R are true, but R is not the correct explanation of A
c) A is true, but R is false
d) A is false, but R is true
Answer: a
R
65. Assertion (A): The BETWEEN operator is inclusive of boundary values. Reason (R): BE-
TWEEN is used to test if a value lies between a specified range.
L
a) Both A and R are true, and R is the correct explanation of A
b) Both A and R are true, but R is not the correct explanation of A
N
c) A is true, but R is false
EE
d) A is false, but R is true
Answer: a
H
66. Assertion (A): You can use the alias keyword AS to rename a column in the output. Reason
AT
(R): Column aliasing affects the actual table structure permanently.
a) Both A and R are true, and R is the correct explanation of A
b) Both A and R are true, but R is not the correct explanation of A
M
Answer: c
67. Assertion (A): NOW() function returns the current system date and time. Reason (R): NOW()
M
Answer: a
O
68. Assertion (A): MID() function is used to extract a part of a string. Reason (R): MID() is a
numeric function used to perform arithmetic operations.
M
69. Assertion (A): The TRIM() function is used to remove trailing spaces only. Reason (R): TRIM()
70. Assertion (A): The IS NULL operator is used to check for zero values. Reason (R): IS NULL
R
checks whether a column has NULL as its value.
a) Both A and R are true, and R is the correct explanation of A
L
b) Both A and R are true, but R is not the correct explanation of A
c) A is false, but R is true
N
d) A is true, but R is false
EE
Answer: c
Answer: YYYY-MM-DD
H
Answer: WHERE
M
9. To display all columns from a table, we use the symbol __________ in a SELECT statement.
Answer: *
10. The command to remove an entire table from the database is __________.
Answer: DROP TABLE
12. The __________ constraint ensures a column cannot have NULL values.
Answer: NOT NULL
13. The __________ function returns the current date and time.
R
Answer: NOW()
L
14. To define a column with fixed length string data, we use the datatype __________.
Answer: CHAR
N
15. The __________ datatype stores numeric values with decimal points.
EE
Answer: FLOAT
18. The __________ constraint automatically fills in a column with a predefined value when no
input is given.
ED
Answer: DEFAULT
19. To rename a column in the output of a SELECT query, we use the keyword __________.
M
Answer: AS
20. The __________ operator is used to check if a column value is within a set of values.
M
Answer: IN
A
Answer: LENGTH()
O
22. The __________ function removes both leading and trailing spaces from a string.
Answer: TRIM()
M
24. The __________ command is used to view the structure of an existing table.
Answer: DESCRIBE
25. The __________ clause is used with SELECT to retrieve only unique values.
Answer: DISTINCT
26. The __________ constraint ensures all values in a column are different.
Answer: UNIQUE
27. The __________ operator is used to retrieve records where a column value lies within a specific
range.
Answer: BETWEEN
R
29. A column that refers to a primary key in another table is called a __________ key.
L
Answer: foreign
30. The __________ command permanently removes all records from a table and the table structure
N
itself.
EE
Answer: DROP
2 MARKS QUESTIONS
H
AT
1. What is SQL?
Answer: SQL stands for Structured Query Language. It is a language used to create, manipulate, and
M
retrieve data from relational databases like MySQL, Oracle, SQL Server, etc.
Answer: SQL is considered easy because its statements are made up of descriptive English words and it
is case-insensitive.
M
Answer: Constraints enforce rules on data in a table to ensure accuracy and integrity, such as NOT NULL,
UNIQUE, PRIMARY KEY, FOREIGN KEY, and DEFAULT.
O
Answer:
Syntax: CREATE DATABASE databasename;
Example: CREATE DATABASE StudentAttendance;
R
RollNumber INT PRIMARY KEY,
SName VARCHAR(20) NOT NULL,
L
SDateofBirth DATE,
GUID CHAR(12)
N
);
EE
8. What is the use of the DESCRIBE command in SQL?
Answer: The DESCRIBE or DESC command is used to view the structure of a table including column
names, data types, and constraints.
H
AT
9. What is a composite primary key? Give an example.
Answer: A composite primary key consists of two or more columns used together to uniquely identify a
record.
M
Example:
PRIMARY KEY (AttendanceDate, RollNumber);
ED
11. How do you add a new column to an existing table? Give syntax and example.
A
Answer:
H
13. Write the syntax to insert data into a table and give an example.
Answer:
Syntax: INSERT INTO tablename VALUES (value1, value2, ...);
Example:
INSERT INTO STUDENT VALUES (1, 'Atharv Ahuja', '2003-05-15', 444444444444);
14. What is the use of the SELECT statement? Give a simple example.
Answer: SELECT is used to retrieve data from a table.
Example:
SELECT * FROM STUDENT;
15. What is the purpose of the DISTINCT clause in a SELECT query?
R
Answer: DISTINCT is used to return only unique (non-duplicate) values in the output of a SELECT
statement.
L
16. Give the syntax of the ORDER BY clause and state its default order.
N
Answer:
Syntax: SELECT * FROM tablename ORDER BY column_name [ASC|DESC];
EE
Default order is ascending (ASC).
17. What is the difference between WHERE and HAVING clause?
H
Answer: WHERE filters rows before grouping, while HAVING filters groups formed by GROUP BY.
AT
18. Write a query to display employee names and their annual income (Salary * 12), using column
aliases.
M
Answer:
FROM EMPLOYEE;
Answer: The LIKE operator is used for pattern matching using wildcards.
Example:
M
3 MARKS QUESTIONS
2. Differentiate between DDL and DML commands with one example each.
Answer:
DDL (Data Definition Language) defines the structure of a database. Example: CREATE TABLE.
DML (Data Manipulation Language) is used to manage data in tables. Example: INSERT INTO.
R
3. Write the syntax of the CREATE TABLE command and give an example to create a STUDENT
L
table with RollNumber, Name, and DateOfBirth.
Answer:
N
Syntax:
EE
CREATE TABLE tablename (column1 datatype, column2 datatype, ...);
Example:
);
ED
3. PRIMARY KEY – Uniquely identifies each record and does not allow NULLs.
H
5. What is a foreign key? Give its syntax and explain with an example.
O
Answer:
A foreign key is a column that refers to the primary key of another table.
M
Syntax:
Example:
6. Write a query to insert a record into the GUARDIAN table. Also, write a query to add a new
column Occupation.
Answer:
Insert:
INSERT INTO GUARDIAN VALUES (444444444444, 'Prithvi Raj', 'M', 'Doctor', 1200000);
Add column:
R
ALTER TABLE GUARDIAN ADD Occupation VARCHAR(30);
L
7. Write a query to update the salary of employee code 103 to 80000 and delete employee with code
104.
N
Answer:
EE
Update:
Delete:
H
AT
DELETE FROM EMPLOYEE WHERE Ecode = 104;
8. What is the difference between DELETE and DROP commands? Give one use case each.
M
Answer:
DELETE removes specific rows but retains the table structure.
ED
9. Explain the use of SELECT statement with WHERE and ORDER BY clauses using an example.
M
Answer:
SELECT retrieves records from a table.
A
Example:
M
10. Write a query using DISTINCT to display unique city names from the CUSTOMER table.
Also, explain its use.
Answer:
Query:
Explanation: DISTINCT is used to remove duplicate values from the result set.
11. Differentiate between the WHERE and HAVING clauses with example queries.
Answer:
WHERE filters rows before grouping;
HAVING filters groups after grouping.
Example:
R
SELECT DeptID, COUNT(*) FROM EMPLOYEE GROUP BY DeptID HAVING COUNT(*) > 2;
L
12. Write queries to demonstrate the use of LIKE operator with the patterns:
a) Names starting with ‘A’
N
b) Names ending with ‘n’
EE
c) Names containing ‘sh’
Answer:
a) SELECT * FROM EMPLOYEE WHERE Ename LIKE 'A%';
b) SELECT * FROM EMPLOYEE WHERE Ename LIKE '%n';
H
AT
c) SELECT * FROM EMPLOYEE WHERE Ename LIKE '%sh%';
13. What are wildcard characters in SQL? Explain % and _ with suitable examples.
M
Answer:
Wildcards are used with LIKE for pattern matching.
ED
Answer:
a) SELECT * FROM SALE WHERE SAmount > 200000;
O
15. List any three aggregate functions in SQL with their purpose.
Answer:
16. Write a query using GROUP BY to count the number of employees in each department.
Answer:
17. Explain the use of MOD() and ROUND() functions with examples.
Answer:
R
MOD(x, y) returns the remainder. Example: MOD(11, 3) returns 2
ROUND(x, y) rounds to ‘y’ decimal places. Example: ROUND(283.28, 1) returns 283.3
L
18. What does the NOW() function return? Also, write a query to retrieve it.
N
Answer:
NOW() returns the current date and time.
EE
Query:
SELECT NOW();
H
AT
19. Write the difference between single-row functions and aggregate functions. Give two examples
of each.
Answer:
M
Answer:
a) SELECT LCASE(Ename) FROM EMPLOYEE;
A
5 MARKS QUESTIONS
M
QUESTION 1
Given Table: STUDENT
R
Write SQL queries for the following:
a. Write a CREATE TABLE command to create the STUDENT table with appropriate data types.
L
N
b. Display all students whose names end with ‘Shah’.
EE
c. Count students born after the year 2002.
H
d. Update the GUID of student with RollNumber 3 to 101010101010.
AT
e. Show the name and birth year of each student.
M
ANSWER: 1
a.
ED
SDateofBirth DATE,
GUID CHAR(12)
M
);
A
b.
H
c.
M
d.
UPDATE STUDENT
SET GUID = '101010101010'
WHERE RollNumber = 3;
e.
QUESTION 2
Given Table: EMPLOYEE
R
E001 Alok 25000 D01
L
E002 Bhavna 30000 D02
E003 Chetan 28000 D01
N
EE
Write SQL queries for the following:
H
a. Write the CREATE TABLE command for EMPLOYEE with relevant types.
AT
b. List employees working in department D01.
M
ANSWER: 2
M
a.
A
Salary INT,
DeptId CHAR(4)
M
);
b.
c.
d.
UPDATE EMPLOYEE
SET Salary = 32000
WHERE Ename = 'Chetan';
e.
R
SELECT * FROM EMPLOYEE
L
ORDER BY Salary DESC;
N
QUESTION 3
Given Table: GUARDIAN
EE
GUID GName GPhone Income
111111111111 Bhutia
H
9812345670 420000
AT
222222222222 Raj Ahuja NULL 310000
M
ANSWER: 3
M
a.
b.
c.
R
d.
L
UPDATE GUARDIAN
SET GPhone = '9012345678'
N
WHERE GUID = '222222222222';
EE
e.
ANSWER: 4
a.
b.
R
SELECT * FROM INVENTORY
WHERE FuelType = 'Petrol';
L
c.
N
SELECT FuelType, COUNT(*) FROM INVENTORY
EE
GROUP BY FuelType;
d.
UPDATE INVENTORY
H
AT
SET Price = 360000.00
WHERE CarId = 'C02';
M
e.
QUESTION 5
M
ANSWER: 5
a.
R
InvoiceNo CHAR(5) PRIMARY KEY,
CarId CHAR(5),
L
CustId CHAR(5),
SalePrice DECIMAL(10,2)
N
);
EE
b.
GROUP BY CarId;
d.
ED
UPDATE SALE
SET SalePrice = 550000.00
M
e.
ORDER BY SalePrice;
H
QUESTION 6
O
R
e. Show all customers sorted alphabetically by name.
L
ANSWER: 6
N
a.
EE
CREATE TABLE CUSTOMER (
CustId CHAR(5) PRIMARY KEY,
CustName VARCHAR(50),
CustAdd VARCHAR(100),
H
AT
Phone CHAR(10),
Email VARCHAR(50)
);
M
b.
ED
c.
M
d.
H
UPDATE CUSTOMER
O
e.
QUESTION 7
Given Table: PRODUCT
R
a. Create the PRODUCT table with correct data types.
L
b. Display all products with stock more than 20.
N
c. Count number of products priced over 5000.
EE
d. Update stock of ‘Monitor’ to 12.
H
AT
e. Show product names sorted by price.
ANSWER: 7
M
a.
Price INT,
Stock INT
M
);
b.
A
c.
M
d.
UPDATE PRODUCT
SET Stock = 12
WHERE ProdName = 'Monitor';
e.
QUESTION 8
Given Table: BOOK
R
B101 Python Programming Sumita Arora 450
L
B102 Data Structures Narayana 550
N
EE
Write SQL queries for the following:
H
AT
b. Show titles priced above 500.
ANSWER: 8
M
a.
A
Title VARCHAR(50),
O
Author VARCHAR(50),
Price INT
M
);
b.
c.
d.
UPDATE BOOK
SET Price = 500
WHERE Title = 'Python Programming';
R
e.
L
ORDER BY Title;
N
QUESTION 9
EE
Given Table: SUPPLIER
ANSWER: 9
a.
b.
c.
R
d.
L
UPDATE SUPPLIER
SET City = 'Noida'
N
WHERE Name = 'Om Supplies';
EE
e.
1 2024-08-01 P
2 2024-08-01 A
M
M
ANSWER: 10
a.
b.
R
WHERE Status = 'P';
c.
L
SELECT COUNT(*) FROM ATTENDANCE
N
WHERE Status = 'A';
EE
d.
UPDATE ATTENDANCE
SET Status = 'P'
H
AT
WHERE RollNo = 2 AND Date = '2024-08-01';
e.
M
QUESTION 11
Given Table: MARKS
M
1 Math 85
A
2 Math 78
H
1 Science 90
O
M
ANSWER: 11
a.
R
RollNo INT,
Subject VARCHAR(30),
L
Marks INT
);
N
b.
EE
SELECT AVG(Marks) AS AvgMarks FROM MARKS
WHERE Subject = 'Math';
c. H
AT
SELECT MAX(Marks) AS MaxScience FROM MARKS
WHERE Subject = 'Science';
M
d.
ED
UPDATE MARKS
SET Marks = 82
WHERE RollNo = 2 AND Subject = 'Math';
M
e.
M
QUESTION 12
Given Table: PRODUCT
O
M
R
e. List all products sorted by name.
L
ANSWER: 12
N
a.
EE
CREATE TABLE PRODUCT (
ProdID CHAR(5) PRIMARY KEY,
Name VARCHAR(30),
Price INT,
H
AT
Quantity INT
);
M
b.
c.
M
d.
M
UPDATE PRODUCT
A
e.
M
QUESTION 13
Given Table: SALES
R
Write SQL queries for the following:
L
a. Create the SALES table.
N
b. Find total sales amount for ‘Pen’.
EE
c. Find the number of sales entries for ‘Notebook’.
ANSWER: 13
a.
ED
Item VARCHAR(30),
Amount INT
M
);
A
b.
H
c.
M
d.
UPDATE SALES
SET Amount = 180
WHERE SaleID = 'S003';
e.
QUESTION 14
Given Table: ATTENDANCE
R
2024-08-01 1 P
L
2024-08-01 2 A
N
2024-08-02 1 P
EE
Write SQL queries for the following:
ANSWER: 14
A
a.
H
RollNo INT,
Status CHAR(1)
M
);
b.
c.
d.
UPDATE ATTENDANCE
SET Status = 'P'
WHERE RollNo = 2 AND Date = '2024-08-01';
R
e.
L
ORDER BY Date, RollNo;
N
QUESTION 15
EE
Given Table: EMPLOYEE
ANSWER: 15
a.
Salary INT
);
b.
c.
R
d.
L
UPDATE EMPLOYEE
N
SET Salary = 46000
WHERE Name = 'Rohan';
EE
e.
ANSWER: 16
a.
R
b.
L
SELECT AVG(Income) AS AverageIncome FROM GUARDIAN;
N
c.
EE
SELECT COUNT(*) AS CountAbove3Lakh FROM GUARDIAN
WHERE Income > 300000;
d.
H
AT
UPDATE GUARDIAN
SET Income = 310000
WHERE GUID = 'G003';
M
e.
ED
QUESTION 17
Given Table: LIBRARY
M
ANSWER: 17
R
a.
L
BookID CHAR(5) PRIMARY KEY,
Title VARCHAR(100),
N
Price INT
EE
);
b.
d.
UPDATE LIBRARY
M
e.
A
QUESTION 18
M
P003 Sharpener 60 6
R
b. Calculate total quantity of items purchased.
L
c. Find maximum rate among all items.
N
EE
d. Update quantity of Eraser to 70.
H
AT
ANSWER: 18
a.
M
Item VARCHAR(30),
Quantity INT,
Rate INT
M
);
b.
M
c.
H
d.
M
UPDATE PURCHASE
SET Quantity = 70
WHERE Item = 'Eraser';
e.
QUESTION 19
Given Table: ORDERS
R
O003 Rohan 4200
L
Write SQL queries for the following:
N
a. Create the ORDERS table.
EE
b. Show total order amount.
ANSWER: 19
a.
M
Amount INT
);
H
b.
O
c.
d.
UPDATE ORDERS
SET Amount = 4600
WHERE OrderID = 'O003';
e.
R
QUESTION 20
Given Table: CLASSMARKS
L
RollNo Subject Marks
N
101 English 88
EE
102 English 92
103 English 80
H
AT
Write SQL queries for the following:
M
ANSWER: 20
O
a.
M
b.
c.
d.
UPDATE CLASSMARKS
SET Marks = 85
WHERE RollNo = 103;
R
e.
L
SELECT * FROM CLASSMARKS
ORDER BY Marks DESC;
N
EE
CHAPTER END EXERCISES
Exercise 1: Answer the following questions Answer: a) RDBMS stands for Relational Database Man-
agement System.
H
AT
Two RDBMS software: MySQL, Oracle
b)
M
c)
Return one result per row Return one result for entire group
A
H
d) Cartesian Product: The result of combining every row from one table with every row from another
O
table.
M
e)
- ALTER changes table structure.
- UPDATE changes table content.
- DELETE removes specific rows.
- DROP removes entire table.
f)
i) DAYNAME()
Exercise 2: Write the output produced by the following SQL statements Answer: a)
SELECT POW(2,3);
→8
R
b)
L
SELECT ROUND(342.9234,-1);
N
→ 340
EE
c)
→ 23
H
AT
d)
↪ MONTHNAME("1979/11/26");
e)
↪ SUBSTR("Practices",3);
M
Exercise 3: Based on MOVIE Table Answer: Given table MOVIE with columns: MovieID, MovieName,
Category, ReleaseDate, ProductionCost, BusinessCost
H
a)
O
b)
c)
d)
e)
SELECT MovieID, MovieName, ProductionCost FROM MOVIE WHERE ProductionCost > 10000 AND
↪ ProductionCost < 100000;
f)
R
SELECT * FROM MOVIE WHERE Category IN ('Comedy', 'Action');
L
g)
N
SELECT * FROM MOVIE WHERE ReleaseDate IS NULL;
EE
Exercise 4: Creating and Modifying Tables in SPORTS Database Answer: a)
b) H
AT
CREATE TABLE TEAM (
TeamID INT CHECK(TeamID BETWEEN 1 AND 9),
M
);
d)
M
DESC TEAM;
A
e)
H
f)
g)
R
Exercise 5: Queries on MATCH_DETAILS Table Answer: a)
L
SELECT MatchID FROM MATCH_DETAILS WHERE FirstTeamScore > 70 AND SecondTeamScore > 70;
N
b)
EE
SELECT MatchID FROM MATCH_DETAILS WHERE FirstTeamScore < 70 AND SecondTeamScore > 70;
c)
H
SELECT MatchID, MatchDate FROM MATCH_DETAILS WHERE FirstTeamID = 1 AND FirstTeamScore
AT
↪ > SecondTeamScore;
d)
M
SELECT MatchID
FROM MATCH_DETAILS
ED
e)
ALTER TABLE COST ADD CONSTRAINT FK_UCode FOREIGN KEY (UCode) REFERENCES
↪ UNIFORM(UCode);
ALTER TABLE UNIFORM ADD CONSTRAINT chk_UName CHECK (UName IS NOT NULL);
ALTER TABLE COST ADD CONSTRAINT chk_price_gt_zero CHECK (Price > 0);
R
Manufacturer VARCHAR(30)
);
L
b) Primary Key: PCode
N
c)
EE
SELECT PCode, PName, UPrice FROM Product ORDER BY PName DESC, UPrice ASC;
d)
H
AT
ALTER TABLE Product ADD Discount INT;
e)
M
OR
M
END;
H
f)
O
g)
h)
i)
j)
k)
R
Exercise 8: Queries on CARSHOWROOM - INVENTORY Table Answer: a)
L
ALTER TABLE INVENTORY ADD Discount INT;
b)
N
UPDATE INVENTORY SET Discount =
EE
CASE
WHEN Model = 'LXI' THEN 0
WHEN Model =
ELSE Price *
'VXI' THEN Price * 0.10
0.12 H
AT
END;
or
M
c)
M
SELECT CarName FROM INVENTORY WHERE FuelType = 'Petrol' ORDER BY Price DESC LIMIT 1;
M
d)
A
e)
O