Sample Paper Two
Sample Paper Two
Question 1(a)
Answer
An RDBMS is a database management system based on the relational model, storing data in tables of rows
and columns, enabling data retrieval, manipulation, and relationships through SQL queries. Two examples of
RDBMS software are MySQL and Oracle Database.
Question 1(b)
(i) ORDER BY
(ii) GROUP BY
Answer
(i) ORDER BY clause is used to sort the result set of a SELECT statement either in ascending (default) or
descending order based on one or more columns. The ASC keyword is used for ascending order, and the
DESC keyword is used for descending order.
(ii) GROUP BY clause is used to group rows that have the same values in specified columns into summary
rows. It is commonly used with aggregate functions (e.g., SUM, COUNT, AVG) to perform calculations on
grouped data.
Question 1(c)
Site any two differences between Single Row Functions and Aggregate Functions.
Answer
Two differences between Single Row Functions and Aggregate Functions are:
Single row functions operate on individual rows and Aggregate functions operate on groups of rows and return a
return a single value per row. single result for each group.
It can be used in SELECT, WHERE, and ORDER BY clause. It can be used in the SELECT clause only.
Question 1(d)
Answer
The Cartesian Product is an operation that combines tuples from two relations. It results in all possible pairs of
rows from the two input relations, regardless of whether they have matching values on common attributes.
This operation is denoted by the cross join symbol (×) in SQL.
Question 1(e)
Answer
It can be used to add, modify, or drop columns, It is used to change the values of one or more columns in a table
constraints, or indexes in a table. based on specified conditions.
The DELETE statement is used to remove one or more The DROP statement is used to remove entire database objects,
rows from a table based on specified conditions. such as tables, views, indexes, or schemas, from the database.
For example, DELETE FROM Employees For example, DROP TABLE Products;
WHERE Department = 'Marketing';
Question 1(f)
1. To display the day like 'Monday', 'Tuesday', from the date when India got independence.
2. To display the specified number of characters from a particular position of the given string.
3. To display the name of the month in which you were born.
4. To display your name in capital letters.
Answer
1. DAYNAME("1947-08-15")
2. SUBSTRING(string, pos, n)
3. MONTHNAME("yyyy-mm-dd")
4. UPPER('YourName')
Question 2
(a)
Output
+-----------+
| POW(2, 3) |
+-----------+
| 8 |
+-----------+
(b)
Output
+---------------------+
| ROUND(342.9234, -1) |
+---------------------+
| 340 |
+---------------------+
(c)
Output
+---------------------------------+
| LENGTH("Informatics Practices") |
+---------------------------------+
| 21 |
+---------------------------------+
(d)
Output
+--------------------+---------------------+-------------------
+-------------------------+
| YEAR("1979/11/26") | MONTH("1979/11/26") | DAY("1979/11/26") |
MONTHNAME("1979/11/26") |
+--------------------+---------------------+-------------------
+-------------------------+
| 1979 | 11 | 26 |
November |
+--------------------+---------------------+-------------------
+-------------------------+
(e)
Output
+------------------+-----------------------------
+--------------------------+------------------------+
| LEFT("INDIA", 3) | RIGHT("ComputerScience", 4) | MID("Informatics",
3, 4) | SUBSTR("Practices", 3) |
+------------------+-----------------------------
+--------------------------+------------------------+
| IND | ence | form
| actices |
+------------------+-----------------------------
+--------------------------+------------------------+
Question 3
Consider the following MOVIE table and write the SQL queries based on it.
(b) List business done by the movies showing only MovieID, MovieName and Total_Earning. Total_Earning to
be calculated as the sum of ProductionCost and BusinessCost.
(d) Find the net profit of each movie showing its MovieID, MovieName and NetProfit. Net Profit is to be
calculated as the difference between Business Cost and Production Cost.
(e) List MovieID, MovieName and Cost for all movies with ProductionCost greater than 10,000 and less than
1,00,000.
(f) List details of all movies which fall in the category of comedy or action.
(g) List details of all movies which have not been released yet.
Answer
(a)
+---------+---------------+-----------+-------------+----------------
+--------------+
| MOVIEID | MOVIENAME | CATEGORY | RELEASEDATE | PRODUCTIONCOST |
BUSINESSCOST |
+---------+---------------+-----------+-------------+----------------
+--------------+
| 1 | Hindi_Movie | Musical | 2018-04-23 | 124500 |
130000 |
| 2 | Tamil_Movie | Action | 2016-05-17 | 112000 |
118000 |
| 3 | English_Movie | Horror | 2017-08-06 | 245000 |
360000 |
| 4 | Bengali_Movie | Adventure | 2017-01-04 | 72000 |
100000 |
| 5 | Telugu_Movie | Action | NULL | 100000 |
NULL |
| 6 | Punjabi_Movie | Comedy | NULL | 30500 |
NULL |
+---------+---------------+-----------+-------------+----------------
+--------------+
(b)
Output
+---------+---------------+---------------+
| MovieID | MovieName | Total_Earning |
+---------+---------------+---------------+
| 1 | Hindi_Movie | 254500 |
| 2 | Tamil_Movie | 230000 |
| 3 | English_Movie | 605000 |
| 4 | Bengali_Movie | 172000 |
+---------+---------------+---------------+
(c)
Output
+-----------+
| Category |
+-----------+
| Musical |
| Action |
| Horror |
| Adventure |
| Comedy |
+-----------+
(d)
SELECT MovieID, MovieName, BusinessCost - ProductionCost AS NetProfit
FROM Movie
WHERE ReleaseDate IS NOT NULL;
Output
+---------+---------------+-----------+
| MovieID | MovieName | NetProfit |
+---------+---------------+-----------+
| 1 | Hindi_Movie | 5500 |
| 2 | Tamil_Movie | 6000 |
| 3 | English_Movie | 115000 |
| 4 | Bengali_Movie | 28000 |
+---------+---------------+-----------+
(e)
Output
+---------+---------------+-------+
| MovieID | MovieName | Cost |
+---------+---------------+-------+
| 4 | Bengali_Movie | 72000 |
| 6 | Punjabi_Movie | 30500 |
+---------+---------------+-------+
(f)
Output
+---------+---------------+----------+-------------+----------------
+--------------+
| MOVIEID | MOVIENAME | CATEGORY | RELEASEDATE | PRODUCTIONCOST |
BUSINESSCOST |
+---------+---------------+----------+-------------+----------------
+--------------+
| 2 | Tamil_Movie | Action | 2016-05-17 | 112000 |
118000 |
| 5 | Telugu_Movie | Action | NULL | 100000 |
NULL |
| 6 | Punjabi_Movie | Comedy | NULL | 30500 |
NULL |
+---------+---------------+----------+-------------+----------------
+--------------+
(g)
+---------+---------------+----------+-------------+----------------
+--------------+
| MOVIEID | MOVIENAME | CATEGORY | RELEASEDATE | PRODUCTIONCOST |
BUSINESSCOST |
+---------+---------------+----------+-------------+----------------
+--------------+
| 5 | Telugu_Movie | Action | NULL | 100000 |
NULL |
| 6 | Punjabi_Movie | Comedy | NULL | 30500 |
NULL |
+---------+---------------+----------+-------------+----------------
+--------------+
Question 4
Suppose your school management has decided to conduct cricket matches between students of Class XI and
Class XII. Students of each class are asked to join any one of the four teams – Team Titan, Team Rockers,
Team Magnet and Team Hurricane. During summer vacations, various matches will be conducted between
these teams. Help your sports teacher to do the following:
1. It should have a column TeamID for storing an integer value between 1 to 9, which refers to unique
identification of a team.
2. Each TeamID should have its associated name (TeamName), which should be a string of length not
less than 10 characters.
(c) Using table level constraint, make TeamID as the primary key.
(d) Show the structure of the table TEAM using a SQL statement.
(e) As per the preferences of the students four teams were formed as given below. Insert these four rows in
TEAM table:
(f) Show the contents of the table TEAM using a DML statement.
(g) Now create another table MATCH_DETAILS and insert data as shown below. Choose appropriate data
types and constraints for each attribute.
Table: MATCH_DETAILS
M1 2018-07-17 1 2 90 86
M2 2018-07-18 3 4 45 48
M3 2018-07-19 1 3 78 56
MatchID MatchDate FirstTeamID SecondTeamID FirstTeamScore SecondTeamScore
M4 2018-07-19 2 4 56 67
M5 2018-07-18 1 4 32 87
M6 2018-07-17 2 3 67 51
Answer
(a)
DESCRIBE TEAM;
Output
+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| TeamID | int | NO | PRI | NULL | |
| TeamName | char(20) | YES | | NULL | |
+----------+----------+------+-----+---------+-------+
(e)
Output
+--------+----------------+
| TeamID | TeamName |
+--------+----------------+
| 1 | Team Titan |
| 2 | Team Rockers |
| 3 | Team Magnet |
| 4 | Team Hurricane |
+--------+----------------+
(g)
Output
Question 5
Using the sports database containing two relations (TEAM, MATCH_DETAILS) and write the queries for the
following:
(a) Display the MatchID of all those matches where both the teams have scored more than 70.
(b) Display the MatchID of all those matches where FirstTeam has scored less than 70 but SecondTeam has
scored more than 70.
(c) Display the MatchID and date of matches played by Team 1 and won by it.
(d) Display the MatchID of matches played by Team 2 and not won by it.
(e) Change the name of the relation TEAM to T_DATA. Also change the attributes TeamID and TeamName to
T_ID and T_NAME respectively.
Answer
(a)
SELECT MatchID
FROM MATCH_DETAILS
WHERE FirstTeamScore > 70 AND SecondTeamScore > 70;
Output
+---------+
| MatchID |
+---------+
| M1 |
+---------+
(b)
SELECT MatchID
FROM MATCH_DETAILS
WHERE FirstTeamScore < 70 AND SecondTeamScore > 70;
Output
+---------+
| MatchID |
+---------+
| M5 |
+---------+
(c)
Output
+---------+------------+
| MatchID | MatchDate |
+---------+------------+
| M1 | 2018-07-17 |
| M3 | 2018-07-19 |
+---------+------------+
(d)
SELECT MatchID
FROM MATCH_DETAILS
WHERE SecondTeamID = 2 AND SecondTeamScore <= FirstTeamScore;
Output
+---------+
| MatchID |
+---------+
| M1 |
+---------+
(e)
Question 6
A shop called Wonderful Garments who sells school uniforms maintains a database SCHOOLUNIFORM as
shown below. It consisted of two relations - UNIFORM and COST. They made UniformCode as the primary
key for UNIFORM relations. Further, they used UniformCode and Size to be composite keys for COST
relation. By analysing the database schema and database state, specify SQL queries to rectify the following
anomalies.
(a) M/S Wonderful Garments also keeps handkerchiefs of red colour, medium size of Rs. 100 each.
(b) INSERT INTO COST (UCode, Size, Price) values (7, 'M', 100);
When the above query is used to insert data, the values for the handkerchief without entering its details in the
UNIFORM relation is entered. Make a provision so that the data can be entered in the COST table only if it is
already there in the UNIFORM table.
(c) Further, they should be able to assign a new UCode to an item only if it has a valid UName. Write a query
to add appropriate constraints to the SCHOOLUNIFORM database.
(d) Add the constraint so that the price of an item is always greater than zero.
Answer
(b)
ALTER TABLE COST ADD CONSTRAINT CK_Price_Positive CHECK (Price > 0);
Question 7
Consider the following table named "Product", showing details of products being sold in a grocery shop.
(a) Create the table Product with appropriate data types and constraints.
(c) List the Product Code, Product name and price in descending order of their product name. If PName is the
same, then display the data in ascending order of price.
(e) Calculate the value of the discount in the table Product as 10 per cent of the UPrice for all those products
where the UPrice is more than 100, otherwise the discount will be 0.
(f) Increase the price by 12 per cent for all the products manufactured by Dove.
Write the output(s) produced by executing the following queries on the basis of the information given above in
the table Product:
Answer
(a)
(c)
Output
+-------+----------------+--------+
| PCode | PName | UPrice |
+-------+----------------+--------+
| P01 | WASHING POWDER | 120 |
| P02 | TOOTHPASTE | 54 |
| P04 | TOOTHPASTE | 65 |
| P03 | SOAP | 25 |
| P05 | SOAP | 38 |
| P06 | SHAMPOO | 245 |
+-------+----------------+--------+
(d)
UPDATE Product
SET Discount = IF(UPrice > 100, (UPrice * (10/100)) + UPrice, 0);
Output
+-------+----------------+--------+--------------+----------+
| PCode | PName | UPrice | Manufacturer | Discount |
+-------+----------------+--------+--------------+----------+
| P01 | WASHING POWDER | 120 | SURF | 12 |
| P02 | TOOTHPASTE | 54 | COLGATE | 0 |
| P03 | SOAP | 25 | LUX | 0 |
| P04 | TOOTHPASTE | 65 | PEPSODENT | 0 |
| P05 | SOAP | 38 | DOVE | 0 |
| P06 | SHAMPOO | 245 | DOVE | 24.5 |
+-------+----------------+--------+--------------+----------+
(f)
UPDATE Product
SET UPrice = (UPrice * (12/100)) + UPrice
WHERE Manufacturer = 'Dove';
Output
+-------+----------------+--------+--------------+----------+
| PCode | PName | UPrice | Manufacturer | Discount |
+-------+----------------+--------+--------------+----------+
| P01 | WASHING POWDER | 120 | SURF | 12 |
| P02 | TOOTHPASTE | 54 | COLGATE | 0 |
| P03 | SOAP | 25 | LUX | 0 |
| P04 | TOOTHPASTE | 65 | PEPSODENT | 0 |
| P05 | SOAP | 43 | DOVE | 0 |
| P06 | SHAMPOO | 274 | DOVE | 24.5 |
+-------+----------------+--------+--------------+----------+
(g)
Output
+--------------+---------------+
| Manufacturer | TotalProducts |
+--------------+---------------+
| SURF | 1 |
| COLGATE | 1 |
| LUX | 1 |
| PEPSODENT | 1 |
| DOVE | 2 |
+--------------+---------------+
(h)
Output
+----------------+-------------+
| PName | avg(UPrice) |
+----------------+-------------+
| WASHING POWDER | 120.0000 |
| TOOTHPASTE | 59.5000 |
| SOAP | 34.0000 |
| SHAMPOO | 274.0000 |
+----------------+-------------+
(i)
Output
+--------------+
| Manufacturer |
+--------------+
| SURF |
| COLGATE |
| LUX |
| PEPSODENT |
| DOVE |
+--------------+
(j)
+-----------------------+
| COUNT(DISTINCT PName) |
+-----------------------+
| 4 |
+-----------------------+
(k)
Output
+----------------+-------------+-------------+
| PName | MAX(UPrice) | MIN(UPrice) |
+----------------+-------------+-------------+
| WASHING POWDER | 120 | 120 |
| TOOTHPASTE | 65 | 54 |
| SOAP | 43 | 25 |
| SHAMPOO | 274 | 274 |
+----------------+-------------+-------------+
Question 8
Using the CARSHOWROOM database given in the chapter, write the SQL queries for the following:
(b) Set appropriate discount values for all cars keeping in mind the following:
(c) Display the name of the costliest car with fuel type "Petrol".
(d) Calculate the average discount and total discount available on Baleno cars.
Answer
Table inventory
(a)
UPDATE INVENTORY
SET Discount = 0
WHERE Model = 'LXI';
UPDATE INVENTORY
SET Discount = Price * 0.10
WHERE Model = 'VXI';
UPDATE INVENTORY
SET Discount = Price * 0.12
WHERE Model NOT IN ('LXI', 'VXI');
Output
+-------+---------+-----------+-----------+-----------------+----------
+------------+----------+
| CarId | CarName | Price | Model | YearManufacture | FuelType
| FinalPrice | Discount |
+-------+---------+-----------+-----------+-----------------+----------
+------------+----------+
| D001 | Dzire | 582613.00 | LXI | 2017 | Petrol
| 652526.60 | 0 |
| D002 | Dzire | 673112.00 | VXI | 2018 | Petrol
| 753885.40 | 67311.2 |
| B001 | Baleno | 567031.00 | Sigma1.2 | 2019 | Petrol
| 635074.70 | 68043.7 |
| B002 | Baleno | 647858.00 | Delta1.2 | 2018 | Petrol
| 725601.00 | 77743 |
| E001 | EECO | 355205.00 | 5 STR STD | 2017 | CNG
| 397829.60 | 42624.6 |
| E002 | EECO | 654914.00 | CARE | 2018 | CNG
| 733503.70 | 78589.7 |
| S001 | SWIFT | 514000.00 | LXI | 2017 | Petrol
| 575680.00 | 0 |
| S002 | SWIFT | 614000.00 | VXI | 2018 | Petrol
| 687680.00 | 61400 |
+-------+---------+-----------+-----------+-----------------+----------
+------------+----------+
(c)
SELECT CarName
FROM INVENTORY
WHERE FuelType = 'Petrol'
AND Price = (SELECT MAX(Price) FROM INVENTORY WHERE FuelType =
'Petrol');
Output
+---------+
| CarName |
+---------+
| Dzire |
+---------+
(d)
Output
+-----------------+----------------+
| AverageDiscount | TotalDiscount |
+-----------------+----------------+
| 72893.33984375 | 145786.6796875 |
+-----------------+----------------+
(e)
SELECT COUNT(*)
FROM INVENTORY
WHERE Discount = 0;
Output
+----------+
| COUNT(*) |
+----------+
| 2 |
+----------+
Prev
Database Concepts
Next
Computer Networks