0% found this document useful (0 votes)
33 views4 pages

Chapter 1 Q & A

The document provides a comprehensive overview of SQL concepts, including definitions of RDBMS, clauses in SELECT statements, and differences between single row and aggregate functions. It also includes SQL commands for creating and manipulating tables, calculating discounts, and querying data from a Product and CARSHOWROOM database. Additionally, it covers operations related to a Streams_Of_Students database, including creating tables, updating records, and performing joins.

Uploaded by

Saily Dhoke
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)
33 views4 pages

Chapter 1 Q & A

The document provides a comprehensive overview of SQL concepts, including definitions of RDBMS, clauses in SELECT statements, and differences between single row and aggregate functions. It also includes SQL commands for creating and manipulating tables, calculating discounts, and querying data from a Product and CARSHOWROOM database. Additionally, it covers operations related to a Streams_Of_Students database, including creating tables, updating records, and performing joins.

Uploaded by

Saily Dhoke
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
You are on page 1/ 4

1.

Answer the following questions:


a) Define RDBMS. Name any two RDBMS software.
RDBMS stands for Relational Database Management System. It stores data in the form of related tables (relations).
Examples: MySQL, Oracle

b) What is the purpose of the following clauses in a SELECT statement?


i) ORDER BY: Used to sort the result set in ascending (ASC) or descending (DESC) order based on one or more
columns.
ii) HAVING: Used to apply a condition on aggregate functions (like COUNT, AVG, etc.) in a GROUP BY clause.

c) Two differences between Single row functions and Aggregate functions:


Single Row Functions Aggregate Functions
Operate on one row at a time Operate on a group of rows
Return one result per row Return one result for a group
Examples: UPPER(), LENGTH() Examples: SUM(), COUNT(), AVG()

d) What do you understand by Cartesian Product?


A Cartesian Product is a combination of every row of one table with every row of another table.
If Table A has m rows and Table B has n rows, the result will have m × n rows.

e) Write the name of the functions to perform the following operations:


i) To display the day like "Monday", "Tuesday":
DAYNAME(date)
ii) To display a specified number of characters from a particular position in a string:
SUBSTRING(string, position, length) or MID(string, position, length)
iii) To display the name of the month:
MONTHNAME(date)
iv) To display your name in capital letters:
UPPER(string)

2. Write the output produced by the following SQL commands:


a) SELECT POW(2,3);
Output: 8
Explanation: 23=82^3 = 8

b)
SELECT ROUND(123.2345, 2),
ROUND(342.9234, -1);
Output: 123.23, 340
Explanation:
• ROUND(123.2345, 2) rounds to 2 decimal places → 123.23
• ROUND(342.9234, -1) rounds to nearest 10 → 340

c) SELECT LENGTH("Informatics Practices");


Output: 22
Explanation: Includes space, so total characters = 22

d)
SELECT YEAR("1979/11/26"),
MONTH("1979/11/26"),
DAY("1979/11/26"),
MONTHNAME("1979/11/26");
Output: 1979, 11, 26, November

e)
SELECT LEFT("INDIA", 3),
RIGHT("Computer Science", 4);
Output: IND, ence
Explanation:
• First 3 characters of "INDIA" → IND
• Last 4 characters of "Computer Science" → ence

f)
SELECT MID("Informatics", 3, 4),
SUBSTR("Practices", 3);
Output: form, actices
Explanation:
• MID("Informatics", 3, 4) → from 3rd character, next 4 → form
• SUBSTR("Practices", 3) → from 3rd character to end → actices

3. Consider the Product table


a) Write SQL queries for the following:
i. Create the table Product with appropriate data types and constraints:
CREATE TABLE Product (PCode VARCHAR(5) PRIMARY KEY,PName VARCHAR(50),UPrice DECIMAL(10,2),
Manufacturer VARCHAR(50));

ii. Identify the primary key in Product:


Primary Key: PCode

iii. 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.
SELECT PCode, PName, UPrice FROM Product ORDER BY PName DESC, UPrice ASC;

iv. Add a new column Discount to the table Product:


ALTER TABLE Product ADD Discount DECIMAL(10,2);

v. Calculate the value of the discount in the table Product as 10% of the UPrice for all those products where the
UPrice is more than 100, otherwise the discount will be 0.
UPDATE Product SET Discount = CASE WHEN UPrice > 100 THEN UPrice * 0.10 ELSE 0 END;

vi. Increase the price by 12% for all the products manufactured by Dove:
UPDATE Product SET UPrice = UPrice * 1.12 WHERE Manufacturer = 'Dove';

vii. Display the total number of products manufactured by each manufacturer:


SELECT Manufacturer, COUNT(*) AS Total_Products FROM Product GROUP BY Manufacturer;

b) Write the output(s) produced by executing the following queries:


i. SELECT PName, AVG(UPrice) FROM Product GROUP BY PName;
Output (approx.):
PName AVG(UPrice)
Washing Powder 120
Tooth Paste 52
Soap 36.5
Shampoo 245

ii. SELECT DISTINCT Manufacturer FROM Product;


Output:
Manufacturer
Surf
Colgate
Lux
Pepsodent
Manufacturer
Dove
iii. SELECT COUNT(DISTINCT PName) FROM Product;
Output: There are 6 entries in the Product table, but PName = Tooth Paste and Soap appear twice.
So, distinct PName values:
• Washing Powder
• Tooth Paste
• Soap
• Shampoo
Total = 4 distinct names
Output: 4

iv. SELECT PName, MAX(UPrice), MIN(UPrice) FROM Product GROUP BY PName;


Grouped by PName:
PName MAX(UPrice) MIN(UPrice)
Washing Powder 120 120
Tooth Paste 65 54
Soap 30 25
Shampoo 245 245
Output:

Washing Powder | 120 | 120


Tooth Paste | 65 | 54
Soap | 30 | 25
Shampoo | 245 | 245

4. Using the CARSHOWROOM database given in the chapter, write the SQL queries for the following:
a) Add a new column Discount in the INVENTORY table.
ALTER TABLE INVENTORY ADD Discount DECIMAL(5,2);

b) Set appropriate discount values for all cars


(i) No discount for LXI model
UPDATE INVENTORY SET Discount = 0 WHERE Model = 'LXI';
(ii) VXI model gives 10% discount
UPDATE INVENTORY SET Discount = 10 WHERE Model = 'VXI';
(iii) 12% discount for all other models
UPDATE INVENTORY SET Discount = 12 WHERE Model NOT IN ('LXI', 'VXI');

c) Display the name of the costliest car with fuel type 'Petrol'
SELECT CarName FROM INVENTORY WHERE FuelType = 'Petrol' ORDER BY Price DESC LIMIT 1;

d) Calculate average and total discount available on Car4


SELECT AVG(Discount) AS Average_Discount, SUM(Discount) AS Total_Discount FROM INVENTORY WHERE CarName
= 'Car4';

e) List the total number of cars having no discount


SELECT COUNT(*) AS No_Discount_Cars FROM INVENTORY WHERE Discount = 0;

Q5: STREAMS_OF_STUDENTS SQL Queries

a) Create the database:


CREATE DATABASE Streams_Of_Students;
b) Create the Student table:
CREATE TABLE Student (AdmNo INT PRIMARY KEY, Name VARCHAR(50), StCode VARCHAR(5));
c) Primary keys and foreign key:
CREATE TABLE Stream (StCode VARCHAR(5) PRIMARY KEY, Stream VARCHAR(30));
-- Foreign key in Student table
ALTER TABLE Student ADD CONSTRAINT fk_stream FOREIGN KEY (StCode) REFERENCES Stream(StCode);
d) Update Jay's stream to Humanities:
UPDATE Student SET StCode = 'S03' WHERE AdmNo = 211;
e) Names ending with 'a', sorted alphabetically:
SELECT Name FROM Student WHERE Name LIKE '%a' ORDER BY Name ASC;
f) Students in Science and Humanities, sorted:
SELECT * FROM Student WHERE StCode IN ('S01', 'S03') ORDER BY Name ASC, AdmNo ASC;
g) Streams with more than 1 student:
SELECT StCode, COUNT(*) AS TotalStudents FROM Student GROUP BY StCode HAVING COUNT(*) > 1;
h) Students in different streams, sorted by AdmNo descending:
SELECT * FROM Student ORDER BY StCode, AdmNo DESC;
i) Cartesian product and cardinality:
SELECT * FROM Student, Stream;
-- Degree = 3 (Student) + 2 (Stream) = 5
-- Cardinality = 6 (students) * 3 (streams) = 18
j) Add new column TeacherIncharge:
ALTER TABLE Stream ADD TeacherIncharge VARCHAR(50);
k) List all teachers and students (join):
SELECT S.Name AS StudentName, T.TeacherIncharge FROM Student S JOIN Stream T ON S.StCode = T.StCode;
l) Cartesian product again:
• Degree = 5 (Student + Stream with new column)
• Cardinality = 6 students × 3 streams = 18

You might also like