0% found this document useful (0 votes)
29 views15 pages

SQL Exercises

SQL Exercises

Uploaded by

Hassan Elbayya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
29 views15 pages

SQL Exercises

SQL Exercises

Uploaded by

Hassan Elbayya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 15

SQL Exercises

Write an SQL query to print details of workers excluding first names,


“Mark” and “Sam” from Worker table.

The required query is:


Select * from Worker where FIRST_NAME not in (‘Mark’,’Sam’);
SQL DML Write an SQL query to print details of Workers with DEPARTMENT
exercises name as “Admin”.
Ans.
The required query is:
Select * from Worker where DEPARTMENT like 'Admin%';

Write an SQL query to print details of the Workers whose FIRST_NAME


contains ‘a’.
Ans.
The required query is:
Select * from Worker where FIRST_NAME like '%a%';
Write an SQL query to fetch the no. of workers for each department .
Ans.
The required query is:
SELECT DEPARTMENT, count(WORKER_ID) No_Of_Workers FROM
worker GROUP BY DEPARTMENT ;

SQL DML Write an SQL query to show the second highest salary from a table.
Ans.
exercises The required query is:
Select max(Salary) from Worker where Salary not in (Select max(Salary)
from Worker);

Display The names and job_id and salary for


employees hired in 2021

Select last_name, hire_date


From employees
Where hire_date like ‘%21’;
SQL exercises
Question 1 : Write an SQL Query to print the name of the distinct employee whose DOB is
between 01/01/1960 to 31/12/1975.
SELECT DISTINCT EmpName FROM Employees WHERE DOB BETWEEN ‘01/01/1960’ AND
‘31/12/1975’;

Question 2: Write an SQL Query to find an employee whose Salary is equal or greater than
10000.

SELECT EmpName FROM Employees WHERE Salary>=10000;

Question 3: Write an SQL Query to find name of employee whose name Start with ‘M’

SELECT * FROM Employees WHERE EmpName like 'M%’;


SQL exercises

How do you find all employees which are also manager? .


SELECT e.name, m.name
FROM Employee e, Employee m
WHERE e.mgr_id = m.emp_id;

For each employee, show the last name and calculate the number of months between
today and the date on which the employee was hired. Round the number of months up to
the closest whole number.
SELECT IAST_NAME, ROUND (MONTHS BETWEEN ( SYSDATE, hire_date) ) MONTHS
WORKED
FROM employees ;
SQL exercises
SQL Excersises
Write a query to get the current date.
SELECT SYSDATE();

Write a query to fetch details of employees whose EmpLname ends with an alphabet ‘A’ and
contains five alphabets.

SELECT * FROM EmployeeInfo WHERE EmpLname LIKE '____a’;


Write an SQL query to fetch the EmpId and FullName
of all the employees working under Manager with id –
‘986’.

SELECT EmpId, FullName FROM EmployeeDetails WHERE


ManagerId = 986;

Write an SQL query to fetch the different projects available


from the EmployeeSalary table.
SELECT DISTINCT(Project) FROM EmployeeSalary;

Write an SQL query to fetch the count of employees working


in project ‘P1’.
SELECT COUNT(*) FROM EmployeeSalary WHERE Project = 'P1’;

Write an SQL query to fetch those employees who live in


Toronto and work under manager with ManagerId – 321.

SELECT EmpId, City, ManagerId FROM EmployeeDetails WHERE


City='Toronto' AND ManagerId='321';
Write an SQL query to fetch the EmpIds that are present in
both the tables – ‘EmployeeDetails’ and ‘EmployeeSalary.
SELECT EmpId FROM EmployeeDetails where EmpId IN (SELECT
EmpId FROM EmployeeSalary);

Write an SQL query to fetch only even iDs from the table.

SELECT * FROM EmployeeDetails WHERE MOD (EmpId, 2) = 0;


write a SQL query to find the details of salespeople who get the commission in the
range from 0.12 to 0.14
SELECT * FROM salesman WHERE commission BETWEEN 0.12 AND 0.14;
write a SQL query to find the details of salespeople with their commissions and write “Has No
Commission” if commission is null
Select last_name,NVL(commission,’ Has No Commission’)
From employees;

write a SQL query to find all those customers who does not have any grade. Return
customer_id, cust_name, city, grade, salesman_id.
SELECT * FROM customer WHERE grade IS NULL;
• write a SQL query to calculate total purchase amount of all orders.
Return total purchase amount.
SELECT SUM (purch_amt) FROM orders;

write a SQL query to find the highest grade of the customers for each
of the city. Return city, maximum grade having max grade greater
than city of code 14
SELECT city,MAX(grade)
FROM customer
GROUP BY city
Having max(grade) > (select max(grade) from customer where city=14);
• write a SQL query to find the salespersons and customers who live in
same city. Return customer name, salesperson name and salesperson
city.

SELECT customer.cust_name, salesman.name, salesman.city FROM


salesman, customer WHERE salesman.city = customer.city;
Let the user be prompted for an employee id to display the salary and
job_id of that employee.
Select job_id, salary
From employees
Where employee_id = &employee_id;

Display the employee number and last name of all employees who earn
more than the average salary.
Show all employees whose salary is the same as that of employee 215
Let the user be prompted to insert a new department in the major table.
• Mark an intermediate point. Empty the entire table of customers.
Confirm that the table is empty. Commit all pending transactions.
Savepoint xyz;
Delete from customers;
Select * from customers;
Commit;

You might also like