Mekki MOURADI

Data Analyst

Rabat, Maroc

À Propos

Data Analyst Junior avec une solide expérience en marketing numérique et des compétences approfondies dans l’utilisation de Microsoft Excel, SQL, Tableau Software et Python.

SQL CASE Statements

Liens

Compétences & Outils

Description du projet

Apprendre à manipuler les données via les CASE Statements :
  • SQL CASE Statement
  • Multiple conditions to a CASE Statement
  • CASE Statements Aggregate Functions
  • CASE Statements and with Aggregate Functions and Joins
  • Transposing Data using CASE clause

Objectifs de l'analyse

  • Utiliser les instructions CASE pour effectuer des évaluations conditionnelles et manipuler les données pour les besoins de l’analyse.
  1. CASE STATEMENTS
  1. Change M to Male and F to Female in the employees table
				
					SELECT  emp_no,
		CASE gender
			WHEN 'M' THEN 'MALE'
			WHEN 'F' THEN 'FEMALE'
			ELSE 'No Gender'
		END AS gender
FROM employees
LIMIT 10;
				
			
emp_nogender
10001MALE
10002FEMALE
10003MALE
10004MALE
10005MALE
10006FEMALE
10007FEMALE
10008MALE
10009FEMALE
10010FEMALE
  1. Multiple Conditions to a CASE Statement
  1. Create a column called age_category that returs young for ages less than 30, aged for ages greater than 60, and middle aged otherwise
				
					SELECT  customerid,
		age,
		CASE
			WHEN age < 30 THEN 'Young'
			WHEN age > 60 THEN 'Aged'
			ELSE 'Middle Aged'
		END AS age_category
FROM customers
LIMIT 10;
				
			
customeridageage_category
CG-1252067Aged
DV-1304531Middle Aged
SO-2033565Aged
BH-1171020Young
AA-1048050Middle Aged
IM-1507066Aged
HP-1481520Young
PK-1907546Middle Aged
AG-1027018Young
ZD-2192566Aged
  1. Retrieve a list of the number of employees that were employed before 1990, between 1990 and 1995, and after 1995
				
					WITH new_hire AS (
    SELECT 
        CASE
            WHEN EXTRACT(YEAR FROM hire_date) < '1990' THEN 'Before 1990'
            WHEN EXTRACT(YEAR FROM hire_date) BETWEEN '1990' AND '1995' THEN 'Between 1990 and 1995'
            ELSE 'After 1995'
        END AS hire_category,
        emp_no
    FROM employees
)
SELECT hire_category, COUNT(emp_no) AS number_of_employees
FROM new_hire
GROUP BY hire_category
ORDER BY 
    CASE
        WHEN hire_category = 'Before 1990' THEN 1
        WHEN hire_category = 'Between 1990 and 1995' THEN 2
        ELSE 3
    END;

				
			
hire_categorynumber_of_employees
Before 19906428
Between 1990 and 19954533
After 1995836
  1. CASE Statement and Aggregate Functions
  1. Retrieve the average salary of all employees
				
					SELECT emp_no, ROUND(AVG(salary)) AS average_salary
FROM salaries
GROUP BY emp_no
ORDER BY AVG(salary) DESC
LIMIT 10;
				
			
emp_noaverage_salary
18006128619
18997128594
20004128212
21587127106
14523126436
14487125685
11486124462
12149123657
15039123643
21413121109
  1. Retrieve a list of the average salary of employees, if the average is more than 64k and less than 80k then return 'Paid Well', if the average is less than 64k then return 'Underpaid' else return 'Manager'
				
					SELECT  emp_no,
        ROUND(AVG(salary)) AS average_salary,
		CASE
			WHEN AVG(salary) BETWEEN 64000 AND 80000 THEN 'Paid Well'
			WHEN AVG(salary)<64000 THEN 'Underpaid'
			ELSE 'Manager'
		END AS salary_category
FROM salaries
GROUP BY emp_no
LIMIT 10;
				
			
emp_noaverage_salarysalary_category
1000175389Paid Well
1000268855Paid Well
1000343030Underpaid
1000456512Underpaid
1000587276Manager
1000650515Underpaid
1000770827Paid Well
1000849308Underpaid
1000978285Paid Well
1001076723Paid Well
  1. Count the number of employees in each salary category
				
					WITH emp_per_cat AS (
SELECT	CASE
			WHEN AVG(salary) BETWEEN 64000 AND 80000 THEN 'Paid Well'
			WHEN AVG(salary)<64000 THEN 'Underpaid'
			ELSE 'Manager'
			END AS salary_category,
		emp_no
FROM salaries
GROUP BY emp_no)
SELECT salary_category, COUNT(emp_no) AS count_emp
FROM emp_per_cat
GROUP BY salary_category
				
			
salary_categorycount_emp
Manager1617
Underpaid7438
Paid Well2742
  1. CASE Statement together with SQL Joins and Aggregate Functions
  1. Retrieve a list of all salaries earned by an employee
				
					SELECT e.emp_no, s.salary
FROM employees e
JOIN salaries s
ON e.emp_no = s.emp_no
GROUP BY e.emp_no, s.salary
LIMIT 10;
				
			
emp_nosalary
1324565477
1913953089
1395190603
1147470084
1789164534
1704454244
1343950723
1113049148
1638652187
2039871624
  1. Retrieve a list of employee number, first and last name, add a column named salary difference which is the difference between the employees max and min salary, a column called salary_increase which returns salary was raised by more than 30k if the difference is >30k, salary was raised by more than 20k if between 20k and 30k, and salary was raised by less than 20k if the difference is <20k
				
					SELECT  e.emp_no,
        e.first_name,
        e.last_name, MAX(s.salary)-MIN(s.salary) AS salary_difference,
	    CASE
	    	WHEN MAX(s.salary)-MIN(s.salary) >=30000 THEN 'Salary was raised by more than 30k$'
	    	WHEN MAX(s.salary)-MIN(s.salary) >= 20000 AND MAX(s.salary)-MIN(s.salary) <30000 THEN 'Salary was raised by more than 20k$'
	    	ELSE 'Salary was raised by less than 20k$'
		END AS salary_increase
FROM employees e
JOIN salaries s
ON e.emp_no=s.emp_no
GROUP BY e.emp_no
LIMIT 10;
				
			
emp_nofirst_namelast_namesalary_differencesalary_increase
10001GeorgiFacello28841Salary was raised by more than 20k$
10002BezalelSimmel6699Salary was raised by less than 20k$
10003PartoBamford3693Salary was raised by less than 20k$
10004ChirstianKoblick34003Salary was raised by more than 30k$
10005KyoichiMaliniak16464Salary was raised by less than 20k$
10006AnnekePreusig20098Salary was raised by more than 20k$
10007TzvetanZielinski31346Salary was raised by more than 30k$
10008SaniyaKalloufi5997Salary was raised by less than 20k$
10009SumantPeac33514Salary was raised by more than 30k$
10010DuangkaewPiveteau7836Salary was raised by less than 20k$
  1. Extract the employee number, first and last namle of the first 100 employees, and add a fourth column called current_employee saying is still employed if the employee is still working in the company, or not an employee anymore if the employee is not working at the company anymore
				
					SELECT  e.emp_no,
        e.first_name,
        e.last_name,
    	CASE
	    	WHEN de.to_date IS NULL THEN 'Still Employed'
	    	ELSE 'Not an employee anymore'
		END AS current_employee
FROM employees e
JOIN dept_emp de
ON e.emp_no=de.emp_no
ORDER BY e.emp_no
LIMIT 100;
				
			
emp_nofirst_namelast_namecurrent_employee
10001GeorgiFacelloStill Employed
10002BezalelSimmelStill Employed
10003PartoBamfordStill Employed
10004ChirstianKoblickStill Employed
10005KyoichiMaliniakStill Employed
10006AnnekePreusigStill Employed
10007TzvetanZielinskiStill Employed
10008SaniyaKalloufiNot an employee anymore
10009SumantPeacStill Employed
10010DuangkaewPiveteauNot an employee anymore
10010DuangkaewPiveteauStill Employed
10011MarySluisNot an employee anymore
10012PatricioBridglandStill Employed
10013EberhardtTerkkiStill Employed
10014BerniGeninStill Employed
10015GuoxiangNooteboomNot an employee anymore
10016KazuhitoCappellettiStill Employed
10017CristinelBouloucosStill Employed
10018KazuhidePehaStill Employed
10018KazuhidePehaNot an employee anymore
10019LillianHaddadiStill Employed
10020MayukoWarwickStill Employed
10021RamziErdeNot an employee anymore
10022ShahafFamiliStill Employed
10023BojanMontemayorStill Employed
10024SuzettePetteyStill Employed
10025PrasadramHeyersNot an employee anymore
10026YongqiaoBerztissStill Employed
10027DivierReistadStill Employed
10028DomenickTempestiNot an employee anymore
10029OtmarHerbstNot an employee anymore
10029OtmarHerbstStill Employed
10030ElvisDemeyerStill Employed
10031KarstenJoslinStill Employed
10032JeongReistadStill Employed
10033ArifMerloNot an employee anymore
10034BaderSwanNot an employee anymore
10035AlainChappeletStill Employed
10036AdamantiosPortugaliStill Employed
10037PradeepMakruckiStill Employed
10038HuanLortzStill Employed
10039AlejandroBrenderStill Employed
10040WeiyiMeristeNot an employee anymore
10040WeiyiMeristeStill Employed
10041UriLenartStill Employed
10042MagyStamatiouNot an employee anymore
10043YishayTzvieliStill Employed
10044MingsenCasleyStill Employed
10045MossShanbhogueStill Employed
10046LucienRosenbaumStill Employed
10047ZvonkoNyanchamaStill Employed
10048FlorianSyrotiukNot an employee anymore
10049BasilTramerStill Employed
10050YinghuaDredgeNot an employee anymore
10050YinghuaDredgeStill Employed
10051HidefumiCaineStill Employed
10052HepingNitschStill Employed
10053SanjivZschocheStill Employed
10054MayumiSchuellerStill Employed
10055GeorgyDredgeNot an employee anymore
10056BrendonBerniniStill Employed
10057EbbeCallawayStill Employed
10058BerhardMcFarlinStill Employed
10059AlejandroMcAlpineStill Employed
10060BreanndaBillingsleyNot an employee anymore
10060BreanndaBillingsleyStill Employed
10061TseHerberStill Employed
10062AnooshPeynStill Employed
10063GinoLeonhardtStill Employed
10064UdiJanschNot an employee anymore
10065SatosiAwdehStill Employed
10066KweeSchuslerStill Employed
10067ClaudiStavenowStill Employed
10068CharleneBrattkaStill Employed
10069MargaretaBiermanStill Employed
10070ReuvenGariglianoNot an employee anymore
10070ReuvenGariglianoStill Employed
10071HisaoLipnerStill Employed
10072HironobySidouStill Employed
10073ShirMcClurgNot an employee anymore
10074MokhtarBernatskyStill Employed
10075GaoDolinskyNot an employee anymore
10076ErezRitzmannStill Employed
10077MonaAzumaStill Employed
10078DanelMondadoriStill Employed
10079KshitijGilsStill Employed
10080PremalBaekNot an employee anymore
10080PremalBaekStill Employed
10081ZhongweiRosenStill Employed
10082ParvizLortzNot an employee anymore
10083VishvZocklerStill Employed
10084TuvalKalloufiStill Employed
10085KenrokuMalabarbaStill Employed
10086SomnathFooteStill Employed
10087XinglinEugenioNot an employee anymore
10088JungsoonSyrzyckiNot an employee anymore
10088JungsoonSyrzyckiStill Employed
10089SudharsanFlastersteinStill Employed
10090KendraHoftingNot an employee anymore
10091AmabileGomatamStill Employed
  1. Transposing Data using the CASE Clause
  1. Retrieve the count of the different profit category from the sales table
				
					WITH pr_cat AS
(SELECT  order_line,
        profit,
		CASE
			WHEN profit<=0 THEN 'No profit'
			WHEN profit>0 AND profit<=500 THEN 'Low profit'
			WHEN profit>500 AND profit<=1500 THEN 'Good profit'
			ELSE 'High profit'
		END AS profit_category
FROM sales)
SELECT profit_category, COUNT(order_line)
FROM pr_cat
GROUP BY profit_category
				
			
profit_categorycount
Good profit97
No profit1936
Low profit7941
High profit20
  1. Transpose the output above
				
					SELECT  SUM(CASE WHEN profit<=0 THEN 1 ELSE 0 END) AS no_profit,
		SUM(CASE WHEN profit>0 AND profit<=500 THEN 1 ELSE 0 END) AS low_profit,
		SUM(CASE WHEN profit>500 AND profit<=1500 THEN 1 ELSE 0 END) AS good_profit,
		SUM(CASE WHEN profit>1500 THEN 1 ELSE 0 END) AS high_profit
FROM sales
				
			
no_profitlow_profitgood_profithigh_profit
193679419720
  1. Retrieve the number of employees in the first 4 departments in the dept_emp table and the departments names
				
					SELECT  de.dept_no,
        COUNT(de.emp_no),
        d.dept_name
FROM dept_emp de
JOIN departments d
ON de.dept_no=d.dept_no
WHERE de.dept_no IN ('d001','d002','d003','d004')
GROUP BY de.dept_no, d.dept_name
				
			
dept_nocountdept_name
d003699Human Resources
d001753Marketing
d0042965Production
d002712Finance
  1. Transpose the output above
				
					SELECT SUM(CASE WHEN dept_no='d001' THEN 1 ELSE 0 END) AS Marketing,
		SUM(CASE WHEN dept_no='d002' THEN 1 ELSE 0 END) AS Finance,
		SUM(CASE WHEN dept_no='d003' THEN 1 ELSE 0 END) AS Human_Resources,
		SUM(CASE WHEN dept_no='d004' THEN 1 ELSE 0 END) AS Production
FROM dept_emp
				
			
marketingfinancehuman_resourcesproduction
7537126992965