Computer Science

Consider the table ORDERS as given below

O_IdC_NameProductQuantityPrice
1001JitendraLaptop112000
1002MustafaSmartphone210000
1003DhwaniHeadphone11500

Note: The table contains many more records than shown here.

A) Write the following queries:

(I) To display the total Quantity for each Product, excluding Products with total Quantity less than 5.

(II) To display the orders table sorted by total price in descending order.

(III) To display the distinct customer names from the Orders table.

(IV) Display the sum of Price of all the orders for which the quantity is null.

OR

B) Write the output

(I) Select c_name, sum(quantity) as total_quantity from orders group by c_name;

(II) Select * from orders where product like '%phone%';

(III) Select o_id, c_name, product, quantity, price from orders where price between 1500 and 12000;

(IV) Select max(price) from orders;

SQL Queries

8 Likes

Answer

(A)

(I)

SELECT Product, SUM(Quantity)
FROM ORDERS
GROUP BY Product
HAVING SUM(Quantity) >= 5;

(II)

SELECT *
FROM ORDERS
ORDER BY Price DESC;

(III)

SELECT DISTINCT C_Name
FROM ORDERS;

(IV)

SELECT SUM(Price) AS Total_Price
FROM ORDERS
WHERE Quantity IS NULL;

OR

(B)

(I) Select c_name, sum(quantity) as total_quantity from orders group by c_name;

Output
+----------+----------------+
| c_name   | total_quantity |
+----------+----------------+
| Jitendra |              1 |
| Mustafa  |              2 |
| Dhwani   |              1 |
+----------+----------------+

(II) Select * from orders where product like '%phone%';

Output
+------+---------+------------+----------+-------+
| O_Id | C_Name  | Product    | Quantity | Price |
+------+---------+------------+----------+-------+
| 1002 | Mustafa | Smartphone |        2 | 10000 |
| 1003 | Dhwani  | Headphone  |        1 |  1500 |
+------+---------+------------+----------+-------+

(III) Select o_id, c_name, product, quantity, price from orders where price between 1500 and 12000;

Output
+------+----------+------------+----------+-------+
| o_id | c_name   | product    | quantity | price |
+------+----------+------------+----------+-------+
| 1001 | Jitendra | Laptop     |        1 | 12000 |
| 1002 | Mustafa  | Smartphone |        2 | 10000 |
| 1003 | Dhwani   | Headphone  |        1 |  1500 |
+------+----------+------------+----------+-------+

(IV) Select max(price) from orders;

Output
+------------+
| max(price) |
+------------+
|      12000 |
+------------+

Answered By

7 Likes


Related Questions