w3resource

SQL Challenges-1: Find the employees in department Sales who solved the cases for all quarters are more than 1200

SQL Challenges-1: Exercise-57 with Solution

From the following table write a query in SQL find the employees in department Sales who solved the cases for all quarters are more than 1200. Return the name of the employees and total cases solved for all quarter. Result should be in ascending order or employee name.

Input:

Table: employees

Structure:

FieldTypeNullKeyDefaultExtra
emp_idintNOPRI
emp_namevarchar(30)YES
emp_sexvarchar(1)YES
depart_namevarchar(25)YES
emp_salaryintYES
qtr1intYES
qtr2intYES
qtr3intYES
qtr4intYES
emp_departmentintYES

Data:

emp_idemp_nameemp_sexdepart_nameemp_salary qtr1qtr2qtr3qtr4emp_department
100StevenMProduction2400024031027530090
101NeenaFProduction1700027030027529090
102LexMAudit1700030029028531080
103AlexanderMMarketing9000 25 270 260 280 60
104BruceMMarketing6000 300 280 275 290 60
105DavidMAudit4800 200 220 250 270 80
106ValliFMarketing4800 300 320 330 350 60
107DianaFMarketing4200 280 270 310 320 60
108NancyMAdministration12000 260 280 300 310 100
109DanielFAdministration9000 220 210 240 260 100
110JohnMAdministration8200 300 290 280 320 100
111IsmaelMAdministration7700 280 300 270 310 100
112Jose ManuelMAdministration7800 250 280 260 300 100
113LuisFAdministration6900 300 280 270 310 100
114DenMSales11000 280 290 300 320 30
115AlexanderMSales3100 310 300 320 340 30
116ShelliFSales2900 245 260 280 300 30
117SigalFSales2800 250 370 290 320 30
133JasonMExport3300 280 270 300 290 50
134MichaelFExport2900 260 280 290 300 50
135KiFExport2400 240 260 270 290 50

Sample Solution:

SQL Code(MySQL):

create table employees (
emp_id integer(4) not null unique,
emp_name varchar(30),
emp_sex varchar(1),
depart_name varchar(25),
emp_salary int(6),
qtr1 int(4),
qtr2 int(4),
qtr3 int(4),
qtr4 int(4),
emp_department int(3));



insert into employees values(100,'Steven     ','M','Production',24000,240,310,275,300,  90);
insert into employees values(101,'Neena      ','F','Production',17000,270,300,275,290,  90);
insert into employees values(102,'Lex        ','M','Audit',17000,300,290,285,310,  80);
insert into employees values(103,'Alexander  ','M','Marketing', 9000,25,270,260,280,  60);
insert into employees values(104,'Bruce      ','M','Marketing', 6000,300,280,275,290,  60);
insert into employees values(105,'David      ','M','Audit', 4800,200,220,250,270,  80);
insert into employees values(106,'Valli      ','F','Marketing', 4800,300,320,330,350,  60);
insert into employees values(107,'Diana      ','F','Marketing', 4200,280,270,310,320,  60);
insert into employees values(114,'Den        ','M','Sales',11000,280,290,300,320 ,  30);
insert into employees values(115,'Alexander  ','M','Sales', 3100,310,300,320,340,  30);
insert into employees values(116,'Shelli     ','F','Sales', 2900,245,260,280,300,  30);
insert into employees values(117,'Sigal      ','F','Sales', 2800,250,370,290,320,  30);
insert into employees values(108,'Nancy      ','M','Administration',12000,260,280,300,310, 100);
insert into employees values(109,'Daniel     ','F','Administration', 9000,220,210,240,260, 100);
insert into employees values(110,'John       ','M','Administration', 8200,300,290,280,320, 100);
insert into employees values(111,'Ismael     ','M','Administration', 7700,280,300,270,310, 100);
insert into employees values(112,'Jose Manuel','M','Administration', 7800,250,280,260,300, 100);
insert into employees values(113,'Luis       ','F','Administration', 6900,300,280,270,310, 100);
insert into employees values(133,'Jason      ','M','Export', 3300,280,270,300,290,  50);
insert into employees values(134,'Michael    ','F','Export', 2900,260,280,290,300,  50);
insert into employees values(135,'Ki         ','F','Export', 2400,240,260,270,290,  50);

SELECT
  emp_name, qtr1+qtr2+qtr3+qtr4 as total_sale
FROM employees
WHERE depart_name = 'Sales'
  AND qtr1+qtr2+qtr3+qtr4> 1200
group by emp_name,total_sale;

Sample Output:

emp_name   |total_cases|
-----------+-----------+
Alexander  |       1270|
Sigal      |       1230|

SQL Code Editor:


Contribute your code and comments through Disqus.

Previous: Department wise number of male and female employees and their total salaries.
Next: Find most expensive and cheapest room from the hotels.



Follow us on Facebook and Twitter for latest update.