Informatics Practices
Consider the CUSTOMERS table having the following records:
Table: CUSTOMERS
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi | 1500.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
4 | Chaitali | 25 | Mumbai | 6500.00 |
5 | Hardik | 27 | Bhopal | 8500.00 |
6 | Komal | 22 | Bengaluru | 4500.00 |
7 | Muffy | 24 | Indore | 10000.00 |
(a) Write an SQL query to display all records in ascending order of name.
(b) Write an SQL query to display all records in descending order of name.
(c) Write an SQL query to display all records in ascending order of name and descending order of age.
(d) Write an SQL query to display maximum salary.
(e) Write an SQL query to display minimum salary.
(f) Write an SQL query to display total number of records.
(g) Write an SQL query to display average salary.
(h) Write an SQL query to display total salary of all the persons.
(i) Write an SQL query to display names of those persons whose salary is greater than the average salary.
(j) Write an SQL query to display details of those persons whose age is less than the average age.
SQL Queries
3 Likes
Answer
(a)
SELECT * FROM CUSTOMERS
ORDER BY NAME;
Output
+----+----------+-----+-----------+--------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+--------+
| 4 | CHAITALI | 25 | MUMBAI | 6500 |
| 5 | HARDIK | 27 | BHOPAL | 8500 |
| 3 | KAUSHIK | 23 | KOTA | 2000 |
| 2 | KHILAN | 25 | DELHI | 1500 |
| 6 | KOMAL | 22 | BENGALURU | 4500 |
| 7 | MUFFY | 24 | INDORE | 10000 |
| 1 | RAMESH | 32 | AHMEDABAD | 2000 |
+----+----------+-----+-----------+--------+
(b)
SELECT * FROM CUSTOMERS
ORDER BY NAME DESC;
Output
+----+----------+-----+-----------+--------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+--------+
| 1 | RAMESH | 32 | AHMEDABAD | 2000 |
| 7 | MUFFY | 24 | INDORE | 10000 |
| 6 | KOMAL | 22 | BENGALURU | 4500 |
| 2 | KHILAN | 25 | DELHI | 1500 |
| 3 | KAUSHIK | 23 | KOTA | 2000 |
| 5 | HARDIK | 27 | BHOPAL | 8500 |
| 4 | CHAITALI | 25 | MUMBAI | 6500 |
+----+----------+-----+-----------+--------+
(c)
SELECT *
FROM CUSTOMERS
ORDER BY NAME ASC, AGE DESC;
Output
+----+----------+-----+-----------+--------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+--------+
| 4 | CHAITALI | 25 | MUMBAI | 6500 |
| 5 | HARDIK | 27 | BHOPAL | 8500 |
| 3 | KAUSHIK | 23 | KOTA | 2000 |
| 2 | KHILAN | 25 | DELHI | 1500 |
| 6 | KOMAL | 22 | BENGALURU | 4500 |
| 7 | MUFFY | 24 | INDORE | 10000 |
| 1 | RAMESH | 32 | AHMEDABAD | 2000 |
+----+----------+-----+-----------+--------+
(d)
SELECT MAX(SALARY) FROM CUSTOMERS;
Output
+-------------+
| MAX(SALARY) |
+-------------+
| 10000 |
+-------------+
(e)
SELECT MIN(SALARY) FROM CUSTOMERS;
Output
+-------------+
| MIN(SALARY) |
+-------------+
| 1500 |
+-------------+
(f)
SELECT COUNT(*) FROM CUSTOMERS;
Output
+----------+
| COUNT(*) |
+----------+
| 7 |
+----------+
(g)
SELECT AVG(SALARY) FROM CUSTOMERS;
Output
+-------------+
| AVG(SALARY) |
+-------------+
| 5000 |
+-------------+
(h)
SELECT SUM(SALARY) FROM CUSTOMERS;
Output
+-------------+
| SUM(SALARY) |
+-------------+
| 35000 |
+-------------+
(i)
SELECT NAME
FROM CUSTOMERS
WHERE SALARY > (SELECT AVG(SALARY) FROM CUSTOMERS);
Output
+----------+
| NAME |
+----------+
| CHAITALI |
| HARDIK |
| MUFFY |
+----------+
(j)
SELECT * FROM CUSTOMERS
WHERE AGE < (SELECT AVG(AGE) FROM CUSTOMERS);
Output
+----+----------+-----+-----------+--------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+--------+
| 2 | KHILAN | 25 | DELHI | 1500 |
| 3 | KAUSHIK | 23 | KOTA | 2000 |
| 4 | CHAITALI | 25 | MUMBAI | 6500 |
| 6 | KOMAL | 22 | BENGALURU | 4500 |
| 7 | MUFFY | 24 | INDORE | 10000 |
+----+----------+-----+-----------+--------+
Answered By
3 Likes
Related Questions
Consider the table RESULT given below. Write commands in MySQL for (a) to (d) and output for (e) to (g):
Table: RESULT
No Name Stipend Subject Average Division 1 Sharon 400 English 38 THIRD 2 Amal 680 Mathematics 72 FIRST 3 Vedant 500 Accounts 67 FIRST 4 Shakeer 200 Informatics 55 SECOND 5 Anandha 400 History 85 FIRST 6 Upasna 550 Geography 45 THIRD (a) To list the names of those students who have obtained Division as FIRST in the ascending order of NAME.
(b) To display a report listing Name, Subject and Annual Stipend received assuming that the Stipend column has monthly stipend.
(c) To count the number of students who have either Accounts or Informatics as subject.
(d) To insert a new row in the table RESULT: 7, "Mohan", 500, "English", 73, "Second"
(e) SELECT AVG(STIPEND) FROM RESULT WHERE DIVISION = "THIRD";
(f) SELECT COUNT(DISTINCT Subject) FROM RESULT;
(g) SELECT MIN(Average) FROM RESULT WHERE Subject = "English";
Write the SQL query commands based on the following table:
Table: SCHOOLBUS
Rtno Area_Covered Capacity NoOfStudents Distance Transporter Charges 1 Vasant Kunj 100 120 10 Shivam Travels 100000 2 Hauz Khas 80 80 10 Anand Travels 85000 3 Pitampura 60 55 30 Anand Travels 60000 4 Rohini 100 90 35 Anand Travels 100000 5 Yamuna Vihar 50 60 20 Bhalla Co. 55000 6 Krishna Nagar 70 80 30 Yadav Co. 80000 7 Vasundhra 100 110 20 Yadav Co. 100000 8 Paschim Vihar 40 40 20 Speed Travels 55000 9 Saket 120 120 10 Speed Travels 100000 10 Janak Puri 100 100 20 Kisan Tours 95000 (a) To show all information of students where capacity is more than the number of students in order of rtno.
(b) To show area_covered for buses covering more than 20 km, but charge is less than 80000.
(c) To show transporter-wise total no. of students travelling.
(d) To show Rtno, Area_covered and Average cost per student for all routes where average cost per student is — Charges/NoOfStudents
(e) Add a new record with the following data:
(11, "Moti Bagh", 35,32,10, "Kisan Tours", 35000)(f) Give the output considering the original relation as given below:
- SELECT SUM(Distance) FROM SCHOOLBUS WHERE Transporter= "Yadav Co.";
- SELECT MIN(NoOfStudents) FROM SCHOOLBUS;
- SELECT AVG(Charges) FROM SCHOOLBUS WHERE Transporter = "Anand Travels";
- SELECT DISTINCT Transporter FROM SCHOOLBUS;
Consider the following tables WORKER and PAYLEVEL and answer the questions:
Table: WORKER
ECODE NAME DESIG PLEVEL DOJ DOB 11 Sachin Patel Supervisor P001 2004-09-13 1985-08-23 12 Chander Nath Operator P003 2010-02-22 1987-07-12 13 Fizza Operator P003 2009-06-14 1983-10-14 15 Ameen Ahmed Mechanic P002 2006-08-21 1984-03-13 18 Sanya Clerk P002 2005-12-19 1983-06-09 Table: PAYLEVEL
PLEVEL PAY ALLOWANCE P001 26000 12000 P002 22000 10000 P003 12000 6000 (a) To display details of all workers in descending order of DOB.
(b) To display the PLEVEL and number of workers in that PLEVEL.
(c) To display the PLEVEL and number of workers in that PLEVEL whose pay is greater than 15000.
(d) To display NAME and DESIG of those workers, whose PLEVEL is either P001 or P002.
(e) Give the output of the following SQL queries:
- SELECT COUNT(PLEVEL), PLEVEL FROM WORKER GROUP BY PLEVEL;
- SELECT MAX(DOB), MIN(DOJ) FROM WORKER;
Consider the tables given below and answer the questions that follow:
Table: EVENT
EventId Event NumPerformers CelebrityID 101 Birthday 10 C102 102 Promotion Party 20 C103 103 Engagement 12 C102 104 Wedding 15 C104 Table: CELEBRITY
CelebrityID CelebrityName Phone FeeCharged C101 Faiz Khan 99101XXXXX 200000 C102 Sanjay Kumar 89346XXXXX 250000 C103 Neera Khan Kapoor 98116XXXXX 300000 C104 Reena Bhatia 70877XXXXX 100000 (a) Name the Primary keys in both the tables and the Foreign key in 'Event' table. Can NumPerformers (Number of performers) be set as the Primary key? Give reason.
(b) How many rows will be present in the Cartesian join of the above-mentioned two tables?
(c) Write the commands in SQL:
- To display EventId, Event name, CelebrityId for only those events that have more than 10 performers.
- To display CelebrityId and names of celebrities who have "Khan" anywhere in their names.
- To display names of celebrities and fee charged for those celebrities who charge more than 200000.