Computer Science
Consider the following tables Employee and Salary. Write SQL commands for the statements (i) to (iv) and give outputs for SQL queries (v) to (vii)
Table : Employee
| Eid | Name | Depid | Qualification | Sec | 
|---|---|---|---|---|
| 1 | Deepali Gupta | 101 | MCA | F | 
| 2 | Rajat Tyagi | 101 | BCA | M | 
| 3 | Hari Mohan | 102 | B.A. | M | 
| 4 | Harry | 102 | M.A. | M | 
| 5 | Sumit Mittal | 103 | B.Tech. | M | 
| 6 | Jyoti | 101 | M.Tech. | F | 
Table : Salary
| Eid | Basic | D.A | HRA | Bonus | 
|---|---|---|---|---|
| 1 | 6000 | 2000 | 2300 | 200 | 
| 2 | 2000 | 300 | 300 | 30 | 
| 3 | 1000 | 300 | 300 | 40 | 
| 4 | 1500 | 390 | 490 | 30 | 
| 5 | 8000 | 900 | 900 | 80 | 
| 6 | 10000 | 300 | 490 | 89 | 
- To display the frequency of employees department wise.
- To list the names of those employees only whose name starts with 'H'
- To add a new column in salary table. The column name is Total_Sal.
- To store the corresponding values in the Total_Sal column.
- Select max(Basic) from Salary where Bonus > 40 ;
- Select count(*) from Employee group by Sex ;
- Select Distinct Depid from Employee ;
SQL Joins & Grouping
24 Likes
Answer
1.
SELECT Depid, COUNT(*) AS Frequency 
FROM Employee 
GROUP BY Depid;
2.
SELECT Name
FROM Employee
WHERE Name LIKE 'H%';
3.
ALTER TABLE Salary
ADD COLUMN Total_Sal FLOAT;
4.
UPDATE Salary
SET Total_Sal = Basic + `D.A.` + HRA + Bonus;
5.
SELECT MAX(Basic)
FROM Salary
WHERE Bonus > 40;
Output
+------------+
| MAX(Basic) |
+------------+
|      10000 |
+------------+
6.
SELECT Sec as sex, COUNT(*) AS Count 
FROM Employee 
GROUP BY Sec ;
Output
+-----+-------+
| sex | Count |
+-----+-------+
| F   |     2 |
| M   |     4 |
+-----+-------+
7.
SELECT DISTINCT Depid FROM Employee ;
Output
+-------+
| Depid |
+-------+
|   101 |
|   102 |
|   103 |
+-------+
Answered By
11 Likes
Related Questions
- Given the following table : - Table : CLUB - COACH-ID - COACHNAME - AGE - SPORTS - DATOFAPP - PAY - SEX - 1 - KUKREJA - 35 - KARATE - 27/03/1996 - 1000 - M - 2 - RAVINA - 34 - KARATE - 20/01/1998 - 1200 - F - 3 - KARAN - 34 - SQUASH - 19/02/1998 - 2000 - M - 4 - TARUN - 33 - BASKETBALL - 01/01/1998 - 1500 - M - 5 - ZUBIN - 36 - SWIMMING - 12/01/1998 - 750 - M - 6 - KETAKI - 36 - SWIMMING - 24/02/1998 - 800 - F - 7 - ANKITA - 39 - SQUASH - 20/02/1998 - 2200 - F - 8 - ZAREEN - 37 - KARATE - 22/02/1998 - 1100 - F - 9 - KUSH - 41 - SWIMMING - 13/01/1998 - 900 - M - 10 - SHAILYA - 37 - BASKETBALL - 19/02/1998 - 1700 - M - Give the output of following SQL statements : - SELECT COUNT(DISTINCT SPORTS) FROM Club ;
- SELECT MIN(Age) FROM CLUB WHERE Sex = 'F' ;
- SELECT AVG(Pay) FROM CLUB WHERE Sports = 'KARATE' ;
- SELECT SUM(Pay) FROM CLUB WHERE Datofapp > '1998-01-31' ;
 
- In a Database, there are two tables given below : - Table : EMPLOYEE - EMPLOYEEID - NAME - SALES - JOBID - E1 - SUMIT SINHA - 1100000 - 102 - E2 - VIJAY SINGH TOMAR - 1300000 - 101 - E3 - AJAY RAJPAL - 1400000 - 103 - E4 - MOHIT RAMNANI - 1250000 - 102 - E5 - SHAILJA SINGH - 1450000 - 103 - Table : JOB - JOBID - JOBTITLE - SALARY - 101 - President - 200000 - 102 - Vice President - 125000 - 103 - Administration Assistant - 80000 - 104 - Accounting Manager - 70000 - 105 - Accountant - 65000 - 106 - Sales Manager - 80000 - Write SQL Queries for the following : - To display employee ids, names of employees, job ids with corresponding job titles.
- To display names of employees, sales and corresponding job titles who have achieved sales more than 1300000.
- To display names and corresponding job titles of those employees who have 'SINGH' (anywhere) in their names.
- Identify foreign key in the table EMPLOYEE.
- Write SQL command to change the JOBID to 104 of the EMPLOYEE with ID as E4 in the table 'EMPLOYEE'.
 
- With reference to following relations PERSONAL and JOB answer the questions that follow : - Create following tables such that Empno and Sno are not null and unique, date of birth is after '12-Jan-1960', name is never blank, Area and Native place is valid, hobby, dept is not empty, salary is between 4000 and 10000. - Table : Personal - Empno - Name - Dobirth - Native-place - Hobby - 123 - Amit - 23-Jan-1965 - Delhi - Music - 127 - Manoj - 12-dec-1976 - Mumbai - Writing - 124 - Abhai - 11-aug-1975 - Allahabad - Music - 125 - Vinod - 04-apr-1977 - Delhi - Sports - 128 - Abhay - 10-mar-1974 - Mumbai - Gardening - 129 - Ramesh - 28-oct-1981 - Pune - Sports - Table : Job - Sno - Area - App_date - Salary - Retd_date - Dept - 123 - Agra - 25-jan-2006 - 5000 - 25-jan-2026 - Marketing - 127 - Mathura - 22-dec-2006 - 6000 - 22-dec-2026 - Finance - 124 - Agra - 19-aug-2007 - 5500 - 19-aug-2027 - Marketing - 125 - Delhi - 14-apr-2004 - 8500 - 14-apr-2018 - Sales - 128 - Pune - 13-mar-2008 - 7500 - 13-mar-2028 - Sales - 129 - Bangalore - 21-july-2003 - 7000 - 21-july-2023 - Finance - (a) Show empno, name and salary of those who have Sports as hobby. - (b) Show name of the eldest employee. - (c) Show number of employee area wise. - (d) Show youngest employees from each Native place. - (e) Show Sno, Name, Hobby and Salary in descending order of Salary. - (f) Show the hobbies of those whose name pronounces as 'Abhay'. - (g) Show the appointment date and native place of those whose name starts with 'A' or ends in 'd'. - (h) Show the salary expense with suitable column heading of those who shall retire after 20-jan-2006. - (i) Show additional burden on the company in case salary of employees having hobby as sports, is increased by 10%. - (j) Show the hobby of which there are 2 or more employees. - (k) Show how many employee shall retire today if maximum length of service is 20 years. - (l) Show those employee name and date of birth who have served more than 17 years as on date. - (m) Show names of those who earn more than all of the employees of Sales dept. - (n) Increase salary of the employees by 5 % of their present salary with hobby as Music or they have completed atleast 3 years of service. - (o) Write the output of : - Select distinct hobby from personal ;
- Select avg(salary) from personal, job where Personal.Empno = Job.Sno and Area in ('Agra','Delhi') ;
- Select count(distinct Native_place) from personal.
- Select name, max(salary) from Personal, Job where Personal.Empno = Job.Sno;
 - (p) Add a new tuple in the table Personal essentially with hobby as Music. - (q) Insert a new column email in Job table - (r) Create a table with values of columns empno, name, and hobby. - (s) Create a view of Personal and Job details of those who have served less than 15 years. - (t) Erase the records of employee from Job table whose hobby is not Sports. - (u) Remove the table Personal. 
- With reference to the table below, answer the questions that follow : - Table : Employees - Empid - Firstname - Lastname - Address - City - 010 - Ravi - Kumar - Raj nagar - GZB - 105 - Harry - Waltor - Gandhi nagar - GZB - 152 - Sam - Tones - 33 Elm St. - Paris - 215 - Sarah - Ackerman - 440 U.S. 110 - Upton - 244 - Manila - Sengupta - 24 Friends street - New Delhi - 300 - Robert - Samuel - 9 Fifth Cross - Washington - 335 - Ritu - Tondon - Shastri Nagar - GZB - 400 - Rachel - Lee - 121 Harrison St. - New York - 441 - Peter - Thompson - 11 Red Road - Paris - Table : EmpSalary - Empid - Salary - Benefits - Designation - 010 - 75000 - 15000 - Manager - 105 - 65000 - 15000 - Manager - 152 - 80000 - 25000 - Director - 215 - 75000 - 12500 - Manager - 244 - 50000 - 12000 - Clerk - 300 - 45000 - 10000 - Clerk - 335 - 40000 - 10000 - Clerk - 400 - 32000 - 7500 - Salesman - 441 - 28000 - 7500 - Salesman - Write the SQL commands for the following using above tables : - (i) To show firstname, lastname, address and city of all employees living in Pairs. - (ii) To display the content of Employees table in descending order of Firstname. - (iii) To display the firstname, lastname and total salary of all managers from the tables Employes and EmpSalary, where total salary is calculated as Salary + Benefits. - (iv) To display the maximum salary among managers and clerks from the table EmpSalary.