Computer Science
Write SQL commands for (i) to (vi) and write output for (vii) on the basis of PRODUCTS relation given below:
Table: PRODUCTS
| PCODE | PNAME | COMPANY | PRICE | STOCK | MANUFACTURE | WARRANTY |
|---|---|---|---|---|---|---|
| P001 | TV | BPL | 10000 | 200 | 2018-01-12 | 3 |
| P002 | TV | SONY | 12000 | 150 | 2017-03-23 | 4 |
| P003 | PC | LENOVO | 39000 | 100 | 2018-04-09 | 2 |
| P004 | PC | COMPAQ | 38000 | 120 | 2019-06-20 | 2 |
| P005 | HANDYCAM | SONY | 18000 | 250 | 2017-03-23 | 3 |
(i) To show details of all PCs with stock more than 110.
(ii) To list the company which gives warranty of more than 2 years.
(iii) To find stock value of the BPL company where stock value is the sum of the products of price and stock.
(iv) To show number of products from each company.
(v) To count the number of PRODUCTS which shall be out of warranty on 20-NOV-2020.
(vi) To show the PRODUCT name of the products which are within warranty as on date.
(vii) Give the output of the following statements:
(a) SELECT COUNT(DISTINCT COMPANY) FROM PRODUCTS;
(b) SELECT MAX(PRICE) FROM PRODUCTS WHERE WARRANTY <= 3;
Answer
(i)
SELECT *
FROM PRODUCTS
WHERE PNAME = 'PC' AND STOCK > 110;
+-------+-------+---------+-------+-------+-------------+----------+
| PCODE | PNAME | COMPANY | PRICE | STOCK | MANUFACTURE | WARRANTY |
+-------+-------+---------+-------+-------+-------------+----------+
| P004 | PC | COMPAQ | 38000 | 120 | 2019-06-20 | 2 |
+-------+-------+---------+-------+-------+-------------+----------+
(ii)
SELECT DISTINCT COMPANY
FROM PRODUCTS
WHERE WARRANTY > 2;
+---------+
| COMPANY |
+---------+
| BPL |
| SONY |
+---------+
(iii)
SELECT COMPANY, SUM(PRICE * STOCK) AS StockValue
FROM PRODUCTS
WHERE COMPANY = 'BPL';
+---------+------------+
| COMPANY | StockValue |
+---------+------------+
| BPL | 2000000 |
+---------+------------+
(iv)
SELECT COMPANY, COUNT(*) AS ProductCount
FROM PRODUCTS
GROUP BY COMPANY;
+---------+--------------+
| COMPANY | ProductCount |
+---------+--------------+
| BPL | 1 |
| SONY | 2 |
| LENOVO | 1 |
| COMPAQ | 1 |
+---------+--------------+
(v)
SELECT COUNT(*)
FROM PRODUCTS
WHERE DATE_ADD(MANUFACTURE, INTERVAL WARRANTY YEAR) <= '2020-11-20';
+----------+
| COUNT(*) |
+----------+
| 2 |
+----------+
(vi)
SELECT PNAME
FROM PRODUCTS
WHERE DATE_ADD(MANUFACTURE, INTERVAL WARRANTY YEAR) >= CURDATE();
There is no output produced because the warranty of all products has expired.
(vii)
(a)
+-------------------------+
| COUNT(DISTINCT COMPANY) |
+-------------------------+
| 4 |
+-------------------------+
(b)
+------------+
| MAX(PRICE) |
+------------+
| 39000 |
+------------+
Related Questions
Differentiate between Primary key and Unique constraints.
Consider the following EMP and DEPT tables:
Table: EMP
EmpNo EmpName City Designation DOJ Sal Comm DeptID 8369 SMITH Mumbai CLERK 1990-12-18 800.00 NULL 20 8499 ANYA Varanasi SALESMAN 1991-02-20 1600.00 300.00 30 8521 SETH Jaipur SALESMAN 1991-02-22 1250.00 500.00 30 8566 MAHADEVAN Delhi MANAGER 1991-04-02 2985.00 NULL 20 Table: DEPT
DeptID DeptName MgrID Location 10 SALES 8566 Mumbai 20 PERSONNEL 9698 Delhi 30 ACCOUNTS 4578 Delhi 40 RESEARCH 8839 Bengaluru Write the SQL command to get the following:
(a) Show the minimum, maximum and average salary of managers.
(b) Count the number of clerks in the organization.
(c) Display the designation-wise list of employees with name, salary and date of joining.
(d) Count the number of employees who are not getting commission.
(e) Show the average salary for all departments having salary > 2000.
(f) List the count of employees grouped by DeptID.
(g) Display the maximum salary of employees in each department.
(h) Display the name of employees along with their designation and department name.
(i) Count the number of employees working in ACCOUNTS department.
Write SQL commands for (i) to (vi) on the basis of relations given below:
Table: BOOKS
Book_ID Book_name Author_name Publishers Price Type qty K0001 Let us C Y. Kanetkar EPB 450 Prog 15 P0001 Computer Networks B. Agarwal FIRST PUBL 755 Comp 24 M0001 Mastering C++ K.R. Venugopal EPB 165 Prog 60 N0002 VC++ advance P. Purohit TDH 250 Prog 45 K0002 Programming with Python Sanjeev FIRST PUBL 350 Prog 30 L02 Computer Science with Python Sumita Arora Dhanpat rai 655 Prog 16 L04 Computer Science with Python Preeti Arora Sultan chand 550 Prog 20 L05 Concise Mathematics R.K.Bansal Selina 600 Maths 10 Table: ISSUED
Book_ID Qty_Issued L02 13 L04 5 L05 21 (i) To show the books of "FIRST PUBL" Publishers written by P.Purohit.
(ii) To display cost of all the books published for FIRST PUBL.
(iii) Depreciate the price of all books of EPB publishers by 5%.
(iv) To display the bookname and price of the books, more than 3 copies of which have been issued.
(v) To show total cost of books of each type.
(vi) To show the details of the costliest book.
What are DDL and DML?