0% found this document useful (0 votes)
20 views11 pages

Grade 12 IP Practical File - DB

The document outlines the creation and population of a MySQL table named 'Products' with specific constraints and sample data. It includes various SQL commands for data manipulation, analysis, and presentation, such as updating product names to uppercase, calculating quantities and prices, filtering data based on price ranges, and performing aggregations. Additionally, it covers commands for sorting and grouping data to gain insights into product pricing and company performance.

Uploaded by

Aashi Phatak
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)
20 views11 pages

Grade 12 IP Practical File - DB

The document outlines the creation and population of a MySQL table named 'Products' with specific constraints and sample data. It includes various SQL commands for data manipulation, analysis, and presentation, such as updating product names to uppercase, calculating quantities and prices, filtering data based on price ranges, and performing aggregations. Additionally, it covers commands for sorting and grouping data to gain insights into product pricing and company performance.

Uploaded by

Aashi Phatak
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/ 11

Database query using MySQL

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

Pcode Pname Qty Price Company

P1001 iPad 120 15000 Apple

P1002 LED TV 100 85000 Sony

P1003 DSLR Camera 10 25000 Philips

P1004 iPhone 50 95000 Apple

P1005 LED TV 20 45000 MI

P1006 Bluetooth Speaker 100 20000 Ahuja

Constraints:

1. Pcode – Primary Key

2. Pname – Not Null

CREATE TABLE Products (Pcode VARCHAR(10) PRIMARY KEY, Pname VARCHAR(255) NOT NULL, Qty
INT, Price DECIMAL(10,2), Company VARCHAR(255))

Explanation:

• This command creates a new table named "Products" in the database.

• 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.

• Qty is an integer representing the quantity of the product.

• Price is a decimal number with precision 10 and scale 2, representing the price of the
product.

• Company is a varchar field to store the name of the company.


Output:

Create table command:

Insert record command.

INSERT INTO Products (Pcode, Pname, Qty, Price, Company) VALUES ('P1001', 'iPad', 120, 15000,
'Apple'), ('P1002', 'LED TV', 100, 85000, 'Sony'), ...

Explanation:

• This command inserts records into the "Products" table.

• Each INSERT INTO statement adds a new row to the table.

• 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.

Convert all product name into capital

UPDATE Products SET Pname = UPPER(Pname)

Explanation:

• This command updates the "Pname" column of the "Products" table.

• It sets the value of "Pname" to its uppercase equivalent using the UPPER() function.

• This ensures that all product names are converted to uppercase.

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:

• This command retrieves data from the "Products" table.

• It selects the "Pname" column and calculates the cube of "Qty" using the POW() function.

• The AS keyword is used to alias the calculated column as "Cube_Qty".

• 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:

• This command retrieves data from the "Products" table.

• It selects the "Pname" column and calculates the division of "Price" by 3.

• The ROUND() function is used to round the result to 1 decimal place.

• The AS keyword is used to alias the calculated column as "Price_Divided".

• 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.

SELECT SUBSTRING(Pname, -4) AS Last_Four_Letters, ROUND(Qty, 2) AS Qty, ROUND(Price, 2) AS


Price, Company FROM Products WHERE Price BETWEEN 30000 AND 80000

Explanation:

• This command retrieves data from the "Products" table.

• 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 AS keyword is used to alias the columns accordingly.

• 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

SELECT MAX(Price) AS Max_Price FROM Products

Explanation:

• This command retrieves data from the "Products" table.

• It calculates the maximum value of the "Price" column using the MAX() function.

• The result is aliased as "Max_Price".

Output

Display the total quantities of all products

SELECT SUM(Qty) AS Total_Quantities FROM Products

Explanation:

• This command retrieves data from the "Products" table.

• It calculates the total sum of the "Qty" column using the SUM() function.

• The result is aliased as "Total_Quantities".

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.

Display the average price of LED TV and Apple products


SELECT AVG(Price) AS Avg_Price FROM Products WHERE Company IN ('LED TV', 'Apple')

Explanation:

• This command retrieves data from the "Products" table.

• 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'.

• The result is aliased as "Avg_Price".

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.

SELECT MAX(Price) - MIN(Price) AS Price_Difference FROM Products

Explanation:

• This command retrieves data from the "Products" table.

• It calculates the difference between the maximum and minimum values of the "Price"
column using the MAX() and MIN() functions.

• The result is aliased as "Price_Difference".

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.

Display unique Products from the table.

SELECT DISTINCT Pname FROM Products


Explanation:

• 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:

11. Aim: Count the unique company from products.

SELECT COUNT(DISTINCT Company) AS Unique_Companies FROM Products

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.

• The result is aliased as "Unique_Companies".

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.

SELECT Pcode, Pname, Company FROM Products ORDER BY Price DESC

Explanation:

• This command retrieves data from the "Products" table.


• It selects the "Pcode", "Pname", and "Company" columns.

• 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.

Display product minimum price for each company.

SELECT Company, MIN(Price) AS Min_Price FROM Products GROUP BY Company

Explanation:

• This command retrieves data from the "Products" table.

• 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:

• This command retrieves data from the "Products" table.

• It selects the "Pcode" and "Pname" columns.

• 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.

Display maximum price of products manufactured by apple.

SELECT MAX(Price) AS Max_Price FROM Products WHERE Company = 'Apple'

Explanation:

• This command retrieves data from the "Products" table.

• It calculates the maximum value of the "Price" column using the MAX() function.

• The WHERE clause filters the rows where the "Company" is 'Apple'.

• The result is aliased as "Max_Price".

Output:

You might also like