Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views2 pages

SQL Functions Exercise Solutions

The document provides exercises and solutions related to SQL functions and RDBMS concepts. It covers definitions, purposes of SQL clauses, differences between single row and aggregate functions, and examples of SQL commands and their outputs. Additionally, it includes a sample product table and SQL queries for creating, modifying, and retrieving data from the table.

Uploaded by

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

SQL Functions Exercise Solutions

The document provides exercises and solutions related to SQL functions and RDBMS concepts. It covers definitions, purposes of SQL clauses, differences between single row and aggregate functions, and examples of SQL commands and their outputs. Additionally, it includes a sample product table and SQL queries for creating, modifying, and retrieving data from the table.

Uploaded by

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

SQL Functions - Exercise Solutions

Q1: Answer the following questions

(a) Define RDBMS. Name any two RDBMS software.


RDBMS (Relational Database Management System) is software that stores and manages
data in the form of related tables.
Examples: MySQL, Oracle, MS SQL Server, PostgreSQL.

(b) Purpose of clauses:


- ORDER BY: Used to sort the result set in ascending (ASC) or descending (DESC)
order.
- HAVING: Used with aggregate functions to filter grouped data.

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


- Single row functions work on one row at a time, return one result per row.
Examples: UPPER(), LENGTH().
- Aggregate functions work on a group of rows, return one result for the group.
Examples: SUM(), AVG().

(d) Cartesian Product:


The result of combining all rows of one table with all rows of another table.
Example: If Table A has 3 rows and Table B has 2 rows → Result will have 6 rows.

(e) Functions to perform operations:


i. Display day like Monday:
SELECT DAYNAME('1947-08-15');
ii. Display specified characters from a string:
SELECT MID('Informatics', 3, 5);
iii. Display month name:
SELECT MONTHNAME('1947-08-15');
iv. Display name in capital letters:
SELECT UPPER('Aman Yadav');

Q2: Write output of SQL commands

(a) SELECT POW(2,3); → 8


(b) SELECT ROUND(123.2345,2), ROUND(342.9234,-1); → 123.23 , 340
(c) SELECT LENGTH('Informatics Practices'); → 21
(d) SELECT YEAR('1979-11-26'), MONTH('1979-11-26'), DAY('1979-11-26'); → 1979 ,
11 , 26
(e) SELECT LEFT('INDIA',3), RIGHT('Computer Science',4); → IND , ence
(f) SELECT MID('Informatics',3,4), SUBSTR('Practices',3); → form , actices

Q3: Table - Product

PCode | PName | UPrice | Manufacturer


P01 | Washing Powder | 120 | Surf
P02 | Tooth Paste | 54 | Colgate
P03 | Soap | 35 | Lux
P04 | Tooth Paste | 65 | Pepsodent
P06 | Shampoo | 245 | Dove

(a) SQL Queries:

i. Create table:
CREATE TABLE Product (
PCode CHAR(3) PRIMARY KEY,
PName VARCHAR(30),
UPrice INT,
Manufacturer VARCHAR(20)
);

ii. Primary key in Product: PCode

iii. List Product Code, Name, Price in descending order of PName, if same then
ascending order of price:
SELECT PCode, PName, UPrice FROM Product ORDER BY PName DESC, UPrice ASC;

iv. Add a new column Discount:


ALTER TABLE Product ADD Discount INT;

v. Calculate discount as 10% if UPrice > 100, else 0:


UPDATE Product SET Discount = IF(UPrice > 100, UPrice*0.10, 0);

vi. Increase price by 12% for Dove products:


UPDATE Product SET UPrice = UPrice*1.12 WHERE Manufacturer='Dove';

vii. Display total products by each manufacturer:


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

(b) Outputs:

i. SELECT PName, AVG(UPrice) FROM Product GROUP BY PName;


-- Gives product names with average prices.

ii. SELECT DISTINCT Manufacturer FROM Product;


-- Gives unique manufacturer names.

You might also like