Grade 12 IP Practical File - DB
Grade 12 IP Practical File - DB
1. AIM : Create and Populate Table: The initial aim is to create a structured table named
"Products" and populate it with sample data. This establishes a foundation for subsequent
queries.
Create the following table products and write queries given below:
Table: Products
Constraints:
CREATE TABLE Products (Pcode VARCHAR(10) PRIMARY KEY, Pname VARCHAR(255) NOT NULL, Qty
INT, Price DECIMAL(10,2), Company VARCHAR(255))
Explanation:
• It defines the structure of the table with columns: Pcode, Pname, Qty, Price, and Company.
• Pcode is set as the primary key, ensuring uniqueness for each row.
• Pname is specified as NOT NULL, meaning it must have a value for each row.
• Price is a decimal number with precision 10 and scale 2, representing the price of the
product.
INSERT INTO Products (Pcode, Pname, Qty, Price, Company) VALUES ('P1001', 'iPad', 120, 15000,
'Apple'), ('P1002', 'LED TV', 100, 85000, 'Sony'), ...
Explanation:
• Values for each column are specified in the VALUES clause, in the order defined in the table
schema
1. To join product and company and display in tabular form like - <pname> manufactured by
<company>
Output:
2. Aim: Data Transformation: Convert all product names into capital letters to ensure
uniformity and ease of comparison in subsequent analysis.
Explanation:
• It sets the value of "Pname" to its uppercase equivalent using the UPPER() function.
Output:
3. Aim: Data Analysis - Quantity: Calculate and display the cube of product quantity for items
with more than 100 in quantity. This helps in exploring the distribution of high-volume products.
Display the cube of products quantity for more than or 100 in quantity.
SELECT Pname, POW(Qty, 3) AS Cube_Qty FROM Products WHERE Qty > 100
Explanation:
• It selects the "Pname" column and calculates the cube of "Qty" using the POW() function.
• The WHERE clause filters the rows where "Qty" is greater than 100.
Output:
4. Aim : Data Analysis - Price: Perform price manipulation by dividing prices by 3 for products priced
higher than 40,000. This allows for normalization of prices and potential cost comparison.
Divide the price by 3 and display the result with 1 fraction digit for price of more than 40,000.
SQL Command:
SELECT Pname, ROUND(Price / 3, 1) AS Price_Divided FROM Products WHERE Price > 40000
Explanation:
• The WHERE clause filters the rows where "Price" is greater than 40,000.
Output:
5. Aim:Data Filtering: Retrieve specific fields (Pname, Qty, Price, Company) for products priced
between 30,000 to 80,000. This filters the dataset to focus on products within a certain price
range.
Display pname (last four letters only), qty, price with 2 decimal points and company for price in
between 30000 to 80000.
Explanation:
• It selects the last four letters of the "Pname" column using the SUBSTRING() function.
• The ROUND() function is used to round the "Qty" and "Price" columns to 2 decimal places.
• The WHERE clause filters the rows where "Price" falls within the range of 30,000 and 80,000.
Output:
6. Aim: Aggregation: Calculate and display the maximum price of products and the total
quantities of all products. This provides insights into the overall pricing and inventory levels.
Display maximum price of products
Explanation:
• It calculates the maximum value of the "Price" column using the MAX() function.
Output
Explanation:
• It calculates the total sum of the "Qty" column using the SUM() function.
Output:
8. Statistical Analysis: Compute the average price of LED TV and Apple products. This helps in
understanding the average pricing trends for specific product categories.
Explanation:
• It calculates the average value of the "Price" column using the AVG() function.
• The WHERE clause filters the rows where the "Company" is either 'LED TV' or 'Apple'.
Output:
9. Aim: Data Comparison: Find the difference between the maximum and minimum prices
from the table. This reveals the price range within the dataset.
Find the difference between maximum price and minimum price from the table.
Explanation:
• It calculates the difference between the maximum and minimum values of the "Price"
column using the MAX() and MIN() functions.
Output:
10. Aim: Data Quality Check: Display unique products and count the unique companies from the
dataset. This ensures data integrity and identifies any potential duplicates.
• This command retrieves unique values from the "Pname" column of the "Products" table.
• The DISTINCT keyword ensures that only unique values are returned, eliminating duplicates.
Output:
Explanation:
• This command retrieves the count of unique values from the "Company" column of the
"Products" table.
• The COUNT() function with DISTINCT keyword ensures that only unique values are counted,
eliminating duplicates.
Output:
12. Aim: Sorting: Sort the products based on price and display product number, name, and
company in descending order. This facilitates easy identification of high-priced products.
Display the product number, product name and company in the descending order of their price.
Explanation:
• The ORDER BY clause orders the results by the "Price" column in descending order.
Output:
13. Aim: Company Analysis: Display the minimum price for each company's product. This offers
insights into the pricing strategies of different companies.
Explanation:
• It groups the data by the "Company" column using the GROUP BY clause.
• Within each group, it calculates the minimum value of the "Price" column using the MIN()
function.
• The result includes the company name and its corresponding minimum price.
Output
14. Aim: Data Presentation: Display product numbers and names in ascending order of names.
This presents the data in a more organized manner.
Display product number and product names in their ascending order of names.
SELECT Pcode, Pname FROM Products ORDER BY Pname ASC
Explanation:
• The ORDER BY clause orders the results by the "Pname" column in ascending order.
Output:
15. Aim: Manufacturer Analysis: Display the maximum price of products manufactured by
Apple. This specifically examines the pricing dynamics of products from a particular manufacturer.
Explanation:
• It calculates the maximum value of the "Price" column using the MAX() function.
• The WHERE clause filters the rows where the "Company" is 'Apple'.
Output: