0% found this document useful (0 votes)
6 views

2 DML Practice

The document provides a comprehensive guide on various SQL query practices, including basic SELECT statements, filtering with LIKE and IN, using BETWEEN for ranges, and employing CASE statements for conditional logic. It also covers numerical and string functions, date and time functions, and sorting techniques to retrieve employee, job, and department data from a database. Additionally, it includes specific examples and scenarios for each SQL function and clause discussed.

Uploaded by

Hasan Emad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

2 DML Practice

The document provides a comprehensive guide on various SQL query practices, including basic SELECT statements, filtering with LIKE and IN, using BETWEEN for ranges, and employing CASE statements for conditional logic. It also covers numerical and string functions, date and time functions, and sorting techniques to retrieve employee, job, and department data from a database. Additionally, it includes specific examples and scenarios for each SQL function and clause discussed.

Uploaded by

Hasan Emad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DML Practice

HR Schema - ERD

Basic SELECT Statement

SELECT [DISTINCT] col1, col2*5 [AS 'newcol2'], col3+col4 [AS 'newcol3'],


function(col5) [AS 'newcol4'], ... ... ...
FROM tablename
[WHERE condition]
[ORDER BY col1 [ASC|DESC], col2 [ASC|DESC], ... ...]
[LIMIT [offset,] rowcount]

• Show all data from countries table.


• Show all data from employees table.
• Show all data from departments table.
• Show all data from job_history table.

LIKE / NOT LIKE

• Show those employee details whose first name starts with the letter s.
• Show those employee details whose first name doesn’t start with the letter s.
• Show those employee details whose first name ends with the letter a.
• Show those employee details whose first name contains da as substring.
• Show those employee details whose first name starts with s and ends with a.
• Show those employee details whose first name either starts with s or starts with m.
• Show those employee details whose first name contains the letter o and a.
• Show those employee details whose first name contains the letter o followed by the letter a.

Mohammad Imam Hossain, Email: [email protected]


• Show those employee details whose first name consists of exactly 3 characters.
• Show those employee details whose first name consists of minimum 3 characters.
• Show those employee details whose first name contains the letter a from the second last position.

IN() / NOT IN()

• Show those country details whose country_id is AU/BR/CN/JP.


• Show those department details whose manager_id is not 204/100/145.
• Show those employee details whose job_id is ST_MAN/IT_PROG.
• Show those employee details who does not work in the department_id 100/30/90.
• Show those location details where the postal_code is either 2901/50090.
• Show those location details where the city name is either Roma/Venice/Tokyo.

BETWEEN … AND… / NOT BETWEEN … AND …

• Show those department details whose location_id is within the range 1000 to 2000 inclusive.
• Show those employee details whose salary is within the range 10000 to 20000 inclusive.
• Show those employee details whose hire_date is within the range ‘1987-01-01’ to ‘1987-06-30’ inclusive.
• Show those employee details whose department_id is not within the range 50 to 60 inclusive.
• Show those job details where the difference between max_salary and min _salary is within the range 5000 to
10000 inclusive.
• Show those job_history details where the end_date is within the range ‘1998-12-01’ to ‘1998-12-31’ inclusive.

CASE WHEN … WHEN … ELSE … END statement

• Show all the past employees employee id, start date, job id and his group name from the job_history table:
Determine the group name as below:

Start Date Group Name


on or before 1989-12-31 C
between 1994-12-31 to 1900-01-01 inclusive B
after 1995-01-01 inclusive A

• Show the department id, department name, location id and location group name from the departments table:
Determine the location group name as below:

Location Id Location Group Name


less than 1200 C
between 2000 to 1200 inclusive B
greater than 2000 A

• From the jobs table show the job id, job title, and job group name.
Determine the job group name based on the following criteria:

JOB_TITLE Job Group Name


job title containing the word “president” President
job title containing the word “manager” Manager
all other jobs Other

Mohammad Imam Hossain, Email: [email protected]


• From the locations table show the location id, city name, state province, and location group name.
Group the locations based on the following criteria:

City Name Location Group Name


Tokyo, Venice, Toronto, Oxford, Singapore A
Mexico City, London, Sydney, Seattle, Beijing B
all other cities C

• Show the EMPLOYEE_ID, START_DATE, END_DATE and SENIORITY_LEVEL from the job_history table where the
SENIOR_LEVEL will be calculated as follow:

Difference between the END_DATE and SENIORITY_LEVEL


START_DATE
diff > 1200 days A
600 days < diff < 1200 days B
diff < 600 days C

• Show the JOB_ID, JOB_TITLE and SALARY_RANGE_CLASS from the jobs table where the SALARY_RANGE_CLASS
will be calculated as follow:

Difference between MAX_SALARY and SALARY_RANGE_CLASS


MIN_SALARY
diff > 20k A
10K < diff <20k B
diff < 10K C

Numerical and String Functions

• Show all the employees employee id and their short name in lowercase format.
Short name format: first 3 letters from the first name followed by an underscore and then followed by the first 3
letters of last name.
• Show all those employee details whose name is a palindrome.
• Show all the employees employee id and email (i.e. add ‘@gmail.com’ at the end of each email).
• Show all the employees first name and phone number.
Phone number format: 515.xxx.xxx7 i.e. only show the first 4 characters and the last character and hide the
intermediate characters with xxx.xxx
• Show all the employees employee id, email and full name.
Full name format: first_name<SPACE>last_name
Also show the full name in 20 characters if necessary right pad with necessary no of spaces.
• Show those location details from locations table whose postal code consists at most 5 characters and the first
two digits of its postal code is between 50 to 99 inclusive.
• Show all the employees employee id, first name and his salary in “10 thousand 5 hundred and 12 taka only”
format.
• For each job, show the job id, job title and how much greater the max_salary from min_salary in percentage
format.
Note: Show the output in 2 decimal points
%greater=(max_salary-min_salary)*100/min_salary
• Show all those job details from jobs table whose salary range (i.e. max_salary-min_salary) is greater than 8000
and the job title contains the word ‘Manager’.

Mohammad Imam Hossain, Email: [email protected]


• Show all the employees employee id, and his yearly total gross salary.
Note: Show the floor value of the total salary
yearly total salary = salary * 12* ( 1+(commission_pct/100)) ]
• Show those department details from departments table whose tens digits of location id is within the range 5 to 9
inclusive.

Date and Time Functions

• Show all the employees email, hire date in “January 4th, 1987” format.
• Show all the employees email, hire date in “Jan 1987, 04” format.
• Show all the employees email, hire date in “1st Aug, 87 05:10 PM” format.
• Show all the employees email, hire date in “15 Jan, 2019 Tuesday 14:10” format.

• Show those employees first name, email, phone number who is hired after the date “05 May, 1987 00:00 AM”.
• Show those employees first name, email, phone number who is hired before the date “1st June 1987 11:01 PM”.

• Show all the employees employee id and his current job experience (till today) in number of years format.
Note: Show the no of years in 3 decimal points.
• Show all the employees employee id, email and his current job experience in “10 years, 06 months and 15 days”
format.
Note: show the months in two digits format if necessary left pad with 0.
• Show all those employees employee id from the job_history table whose job experience is greater than 5 years.

• For each job_history, show how many days an employee has served during his last month of retirement.
• For each employee, show how many days an employee has served during his first month of joining.

• Show all those employee details who have been hired on the leap day(29th Feb) of any leap year.
• Show all those employee details whose hiring month is either 2/4/6/8.
• Show all those employee details who have joined either in the year of 1997 or in the month of February(any
year).

• Show all the employees first name, department id, manager id and his updated join date that is one week before
the real join date.
• From the employees table, show all the employees employee id, join date and estimated retirement date that is
35 years after his join date.

• Show all the employees first name, last name, email, hire date, salary in descending order of salary. If multiple
employees receive the same salary then also sort them based on the alphabetical order of their first name.
• Show all the employees employee id and their join date in such a way that the senior most employee comes first.
If multiple employees have the same join date then also sort them based on the descending order of their
department id.
• Show all the employees first name, email and phone number. Order the output based on the descending order of
first 3 digits of their phone number.
• Show all the employees employee id, email, hire year (only the year portion) and hire month (show the full
month name). Show the output from most recent hired employee to old employees.

Mohammad Imam Hossain, Email: [email protected]


• Show all the job_history details in such a way that senior most employee data comes first and if multiple
employees have the same start date then also sort them based on the descending order of their end date.
• Show all the jobs from jobs table where the highest salary range (i.e. max_salary-min_salary) job data comes
first.

• Show all the distinct manager_ids from employees table.


• Show all the distinct job_ids from the employees table.
• Show all the distinct country_ids from the locations table.
• Show all the distinct job_ids and department_ids from employees table.

• Show the highest salary holder employee details from the employees table.
• Show the top 10 most experienced employee details from the employees table.
• Show the 2nd lowest salary range (i.e. max_salary-min_salary) job details from the jobs table.
• Show the top 3 lowest salary holder employee details from department number 60.
• Among the employees supervised by manager id 108, find out the 2nd highest salary holder employee details.
• Among the employees whose job type is ‘ST_CLERK’, show the highest experienced employee id from the
job_history table.

Mohammad Imam Hossain, Email: [email protected]

You might also like