0% found this document useful (0 votes)
287 views14 pages

SQL Query Exercises for Beginners

The document contains examples of SQL queries for selecting, filtering, sorting, aggregating, and grouping data from employee database tables. Some examples include: 1. Writing queries to select employee names, departments, salaries and calculate metrics like monthly salary and PF from tables like employees, salaries, and dept_emp. 2. Examples of restricting results based on salary ranges, departments, hire dates and other filters. 3. Sorting queries by salary, names, departments and dates. 4. Aggregate functions like count, sum, avg, min, max applied on tables and grouped by departments. 5. Joins used between employee, salary and department tables for filtering results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
287 views14 pages

SQL Query Exercises for Beginners

The document contains examples of SQL queries for selecting, filtering, sorting, aggregating, and grouping data from employee database tables. Some examples include: 1. Writing queries to select employee names, departments, salaries and calculate metrics like monthly salary and PF from tables like employees, salaries, and dept_emp. 2. Examples of restricting results based on salary ranges, departments, hire dates and other filters. 3. Sorting queries by salary, names, departments and dates. 4. Aggregate functions like count, sum, avg, min, max applied on tables and grouped by departments. 5. Joins used between employee, salary and department tables for filtering results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

**************** Basic SELECT statement

1. Write a query to display the names (first_name, last_name) using


alias name “First Name", "Last Name"
R= select first_name "First Name", last_name "Last Name" from employees limit 20;

2. Write a query to get unique department ID from employee table.


R= select distinct dept_no from dept_emp limit 20;

3. Write a query to get all employee details from the employee table
order by first name, descending
R= select * from employees order by first_name desc limit 20;
4. Write a query to get the names (first_name, last_name), salary, PF
of all the employees (PF is calculated as 15% of salary).
R= select employees.emp_no, first_name, last_name, salary, salary*.15 ‘pf’ from
employees inner join employees.emp_no=salaries.emp_no limit 20;

5. Write a query to get the employee ID, names (first_name,


last_name), salary in ascending order of salary.
R= select employees.emp_no, first_name, last_name, salary from employees inner
join employees.emp_no=salaries.emp_no order by salary asc limit 20;

6. Write a query to get the total salaries payable to employees.


R= select sum(salary) as ‘Total Salaries’ from salaries;
7. Write a query to get the maximum and minimum salary from
employees table.
R= select min(salary), max(salary) from salaries;

8. Write a query to get the average salary and number of employees in


the employees table.
R= select max(emp_no), avg(salary) from salaries;

9. Write a query to get the number of employees working with the


company.
R= select count(emp_no) from employees;

10. Write a query to get the number of jobs available in the employees
table.
R= select count(dept_name) ‘Number of Jobs’ from departments;

11. Write a query get all first name from employees table in upper case.
R= select upper(first_name) from employees limit 20;
12. Write a query to get the first 3 characters of first name from
employees table.
R= select first_name from employees limit 3 limit 20;

13. Write a query to calculate 171*214+625.


R= SELECT 171*214+625 Result;

14. Write a query to get the names (for example Ellen Abel, Sundar Ande
etc.) of all the employees from employees table.
R= select concat(first_name,' ', last_name) 'employee name' from
employees limit 20;
15. Write a query to get first name from employees table after removing
white spaces from both side.
R= select trim(first_name) from employees limit 20;

16. Write a query to get the length of the employee names (first_name,
last_name) from employees table.
R= select first_name,last_name, length(first_name)+length(last_name)
'length of names' from employees limit 20;

17. Write a query to check if the first_name fields of the employees table
contains numbers.
R= select * from employees where first_name regexp '[0-9]';

18. Write a query to select first 10 records from a table.


R= select employee_id, first_name from employees limit 10;
19. Write a query to get monthly salary (round 2 decimal places) of each
and every employee
R= select employees.emp_no , first_name, last_name, salary,
round(salary/12,2) ‘Monthly Salary’ from employees inner join salaries on
employees.emp_no=salaries.emp_no limit 20;

***************** Restricting and Sorting data


1. Write a query to display the name (first_name, last_name) and
salary for all employees whose salary is not in the range $10,000
through $15,000
R= select employees.emp_no, first_name, last_name, salary from
employees inner join salaries on employees.emp_no=salaries.emp_no
where salary not between 10000 and 15000 limit 20;
2. Write a query to display the name (first_name, last_name) and
department ID of all employees in departments 30 or 100 in
ascending order.
R= select employees.emp_no, first_name, last_name, dept_no from
employees inner join dept_emp on employees.emp_no=dept_emp.emp_no
where dept_no in(‘d001’, ‘d003’) order by dept_no asc limit 20;

En el ejemplo use de d001 a d003 para que me arrojara algún resultado.


3. Write a query to display the name (first_name, last_name) and
salary for all employees whose salary is not in the range $10,000
through $15,000 and are in department 30 or 100.
R= select first_name, last_name, dept_no, salary from employees inner
join dept_emp on employees.emp_no=dept_emp.emp_no inner join
salaries on salaries.emp_no where dept_no in(‘d006’,’d008’) and salary
not between 10000 and 15000 limit 20;

En el ejemplo use de d003 a d008 para que me arrojara algún resultado.


4. Write a query to display the name (first_name, last_name) and hire
date for all employees who were hired in 1987.
R= select employees.emp_no, first_name, last_name, from_date from
employees inner join dept_emp_latest_date on
employees.emp_no=dept_emp_latest_date.emp_no where
year(from_date) like ‘1987%’ limit 20;

5. Write a query to display the first_name of all employees who have


both "b" and "c" in their first name.
R= select first_name from employees where first_name like ‘%b%’ and
first_name like ‘%c%’ limit 20;
6. Write a query to display the last name, job, and salary for all
employees whose job is that of a Programmer or a Shipping Clerk,
and whose salary is not equal to $4,500, $10,000, or $15,000.
R= select last_name, title, salary from employees inner join salaries on
salaries.emp_no inner join titles on employees.emp_no=titles.emp_no
where title in(‘Staff’,’Engineer’) and salary not in(4500, 10000, 15000) limit
20;

En el ejemplo use de d003 a d008 para que me arrojara algún resultado.


7. Write a query to display the last name of employees whose names
have exactly 6 characters.
R= select first_name as ‘Nombres de seis letras’, last_name from
employees where length(first_name)=6 limit 20;
8. Write a query to display the last name of employees having 'e' as
the third character.
R= select last_name from employees where last_name like ‘__e%’ limit 20;

9. Write a query to display the jobs/designations available in the


employees table.
R= select * from departments;

10. Write a query to display the name (first_name, last_name), salary


and PF (15% of salary) of all employees.
R= select employees.emp_no first_name , last_name, salary, salary*.15 as
‘PF’ from employees inner join salaries on
salaries.emp_no=salaries.emp_no limit 20;
11. Write a query to select all record from employees where last name
in 'BLAKE', 'SCOTT', 'KING' and 'FORD'.
R= select * from employees where last_name
in(‘blake’,’scott’,’king’,’ford’);

******************* Aggregate Functions and Group by

1. Write a query to list the number of jobs available in the employees


table.
R= select count(dept_name) from departments;

2. Write a query to get the total salaries payable to employees.


R= select sum(salary) from salaries;

3. Write a query to get the minimum salary from employees table.


R= select min(salary) from salaries;

4. Write a query to get the maximum salary of an employee working as


a Programmer.
R= select max(salary), dept_no from salaries inner join dept_emp on
salaries.emp_no=dept_emp.emp_no where dept_no=’d001’;
5. Write a query to get the average salary and number of employees
working the department 90.
R= select avg(salary), dept_no from salaries inner join dept_emp on
salaries.emp_no=dept_emp.emp_no where dept_no=’d001’;

6. Write a query to get the highest, lowest, sum, and average salary of
all employees.
R= select max(salary) as ‘max salary’, min(salary) as ‘min salary’,
sum(salary) as ‘sum’, avg(salary) as ‘prom’ from salaries;

7. Write a query to get the number of employees with the same job.
R= select dept_no, count(*) as ‘personas trabajando’ from dept_emp
group by dept_no;

8. Write a query to get the difference between the highest and lowest
salaries.
R= select max(salary), min(salary), max(salary)-min(salary from salaries;
9. Write a query to find the manager ID and the salary of the lowest-
paid employee for that manager.
R= select min(salary, dept_no from dept_emp inner join salaries on
dept_emp.emp_no=salaries.emp_no group by dept_no limit 20;

10. Write a query to get the department ID and the total salary payable
in each department.
R= select sum(salary), dept_no from dept_emp inner join salaries on
dept_emp.emp_no=salaries.emp_no group by dept_no limit 20;

11. Write a query to get the average salary for each job ID excluding
programmer.
R= select avg(salary), dept_no from dept_emp inner join salaries on
dept_emp.emp_no=salaries.emp_no where dept_no not in(‘d001’) group
by dept_no limit 20;

se excluyo a marketing d001


12. Write a query to get the total salary, maximum, minimum, average
salary of employees (job ID wise), for department ID 90 only.
R= select sum(salary), min(salary), max(salary), avg(salary), dept_no from
salaries inner join dept_emp on salaries.emp_no=dept_emp.emp_no
where dept_no=’d001’;

13. Write a query to get the job ID and maximum salary of the
employees where maximum salary is greater than or equal to $4000.
R= select max(salary), emp_no from salaries where salary >= 4000 group
by emp_no limit 20;

14. Write a query to get the average salary for all departments
employing more than 10 employees.
R= select amvg(salary), dept_no, count(*) as ‘numero empleados’ from
dept_emp inner join salaries on dept_emp.emp_no group by
dept_nonhaving count(*) >10;

You might also like