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

SQL Queries

The document contains SQL queries to retrieve data from Oracle database tables like employees, salesman, customer, and orders. The queries select, filter, group, order and perform other operations on the table data. Key queries include getting employee details, total employees, department-wise employee count, salesperson and customer details for the same city, orders between certain amounts, and salesperson commissions over 12%. The document aims to demonstrate common SQL operations on multiple tables.

Uploaded by

Shrutika Agrawal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
411 views

SQL Queries

The document contains SQL queries to retrieve data from Oracle database tables like employees, salesman, customer, and orders. The queries select, filter, group, order and perform other operations on the table data. Key queries include getting employee details, total employees, department-wise employee count, salesperson and customer details for the same city, orders between certain amounts, and salesperson commissions over 12%. The document aims to demonstrate common SQL operations on multiple tables.

Uploaded by

Shrutika Agrawal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Write a Oracle SQL command to display the employee name, job and annual salary for all employees

select FIRST_NAME, LAST_NAME, SALARY*12, JOB_ID from employees;

select FIRST_NAME, LAST_NAME, SALARY*12 “ANNUAL SALARY”, JOB_ID from employees;

Write a Oracle SQL query to get the total number of employees working in the company.

select count(EMPLOYEE_ID) from employees;

select count(EMPLOYEE_ID) "TOTAL EMPLOYEE" from employees;

Write a Oracle SQL query to get the total salary being paid to all employees.

select sum(SALARY) "TOTAL MONTHLY SALARY" FROM EMPLOYEES;

Write a Oracle SQL query to get the maximum and minimum salary from the employees table.

select max(SALARY) "Maximum salary", min(SALARY) "Minimum salary" from employees;

Write a Oracle SQL query to display the name of the employees in order to earning from lowest salary to
highest.

select FIRST_NAME, SALARY from employees order by SALARY DESC;

Write a Oracle SQL query to display the name of the employees in order to alphabetically ascending order.

select FIRST_NAME, LAST_NAME from employees order by FIRST_NAME;

Write a Oracle SQL query to display the name and their annual salary. The result should contain those
employees first who earning the highest salary.

SELECT FIRST_NAME, LAST_NAME, SALARY*12 AS ANNUAL SALARY FROM EMPLOYEES ORDER BY ANNUAL
SALARY DESC;

Write a Oracle SQL query to display department id and total number of employees working in each
department.

select DEPARTMENT_ID, COUNT(EMPLOYEE_ID) FROM employees group by DEPARTMENT_ID;

...to disply only those departments where more than 5 employees are working

select DEPARTMENT_ID, COUNT(EMPLOYEE_ID) FROM employees group by DEPARTMENT_ID having


COUNT(EMPLOYEE_ID)>5;

Write a Oracle SQL query to display the designation (job id) and total number of employees working in each
designation.

SELECT JOB_ID, COUNT(EMPLOYEE_ID) FROM employees group by JOB_ID;

Write a query to list the name of all the employees who are working in department number 20.

select FIRST_NAME, LAST_NAME FROM employees where DEPARTMENT_ID=20;


Write a query to list the employees name and total salary of a year and yearly salary is more than $10000.

Write a query to list the employees name and salary whose daily salary is more than $100.

SELECT FIRST_NAME, SALARY FROM employees WHERE SALARY/30 > 100;

Write a query to list the name of all the employees who are not working in department number 20.

select first_name, last_name from employees where not department_id=20;

Write a query to list the name of all the employees who are working as account manager and drawing a
salary more than $5000.

SELECT FIRST_NAME FROM employees where SALARY > 5000 AND JOB_ID = 'FI_ACCOUNT';

Write a query to list the names of all the employees who are working as accountant in finance section and
drawing a salary less than $20000.

SELECT FIRST_NAME, LAST_NAME FROM employees where SALARY < 20000 AND JOB_ID='FI_ACCOUNT';

Write a query to list the name of all the employees who are working as accountant in finance section and
drawing a salary is greater than equal to $5000 and less than equal to $20000.

SELECT FIRST_NAME, LAST_NAME FROM employees where JOB_ID='FI_ACCOUNT' AND SALARY BETWEEN
5000 AND 20000;

Write a query to list the names, salary of all the employees who are working with a commission package

select FIRST_NAME, SALARY FROM employees where COMMISSION_PCT <> 0;

Write a query to list the name, salary of all the employees where employee first name belongs in a specified
list (“Peter”, “Smith”)

SELECT FIRST_NAME, SALARY FROM employees WHERE FIRST_NAME IN ('Peter','Smith');

Write a query to list the first name, last name, Job id of all the employees except "PRESIDENT" & "MGR" in
asc order of Salaries.

select first_name, last_name, job_id from employees where not job_id like ‘%MGR’ and not job_id like
‘%PRESIDENT’ order by salary;

Write a query to list the names (first and last) of those employees whose name starts with A

SELECT FIRST_NAME FROM employees where FIRST_NAME LIKE 'A%';

Write a query to list the names (first and last) of those employees whose name have second alphabet 's' in
their names.

select FIRST_NAME, LAST_NAME FROM employees where FIRST_NAME LIKE '_s%';

Write a query to list the names (first and last) of those employees whose first name has only five characters.

select FIRST_NAME, LAST_NAME FROM employees where LENGTH(FIRST_NAME)=5;


Write a query to list the names (first and last) of those employees whose first name has only five characters
and starts with 'A'.

select first_name, last_name from employees where length(first_name) = 5 and first_name like ‘A%’;

Write a query to list first_name, last_name of employees with the pattern 'l_x' in their first name.

SELECT FIRST_NAME FROM employees where FIRST_NAME LIKE '%l_x%';

Write a query to list the name (first and last name), hire date of all the employees who joined before or after
2005.

select FIRST_NAME, LAST_NAME FROM employees where TO_CHAR(HIRE_DATE,'YYYY')<> '2005';

Write a query to list the names (first and last), hire date of those employees who joined in the month of
which second character is 'u'.

SELECT first_name, last_name, hire_date FROM employees

WHERE to_char(hire_date, 'month') LIKE '_u%';

To create table salesman

create table salesman(s_id number(4) not null primary key, name varchar2(15), city varchar2(15), commission
number(5,2));

create table salesman(

salesman_id number(4) not null,

name varchar2(15),

city varchar2(15),

commission number(5,2),

constraint salesman_id_pk primary key(salesman_id)

);

To create table customer

create table customer(

customer_id number(4) not null,

cust_name varchar2(15),

city varchar2(15),

grade number(3),
salesman_id number(4),

constraint salesman_id_fk foreign key(salesman_id)

references salesman(salesman_id),

constraint customer_id_pk primary key(customer_id));

To create table orders

create table orders(

o_no number(5),

p_amt number(8,2),

o_date date,

customer_id number(4),

salesman_id number(4),

constraint salesman_id_fk1 foreign key(salesman_id) references salesman(salesman_id),

constraint customer_id_fk foreign key(customer_id) references customer(customer_id),

constraint o_no_pk primary key(o_no));

TO INSERT RECORD IN ORDERS TABLE

insert all

into orders(o_no,p_amt,o_date,customer_id,salesman_id) values(70001,150.5,'05-OCT-2012',3005,5002)

into orders(o_no,p_amt,o_date,customer_id,salesman_id) values(70009,270.65,'10-SEP-2012',3001,5005)

into orders(o_no,p_amt,o_date,customer_id,salesman_id) values(70002,65.26,'05-OCT-2012',3002,5001)

into orders(o_no,p_amt,o_date,customer_id,salesman_id) values(70004,110.5,'07-AUG-2012',3009,5003)

into orders(o_no,p_amt,o_date,customer_id,salesman_id) values(70007,948.5,'10-SEP-2012',3005,5002)

into orders(o_no,p_amt,o_date,customer_id,salesman_id) values(70005,2400.6,'27-JUL-2102',3007,5001)

into orders(o_no,p_amt,o_date,customer_id,salesman_id) values(70008,5760,'10-SEP-2012',3002,5001)

into orders(o_no,p_amt,o_date,customer_id,salesman_id) values(70010,1983.43,'10-OCT-2012',3004,5006)

into orders(o_no,p_amt,o_date,customer_id,salesman_id) values(70003,2480.4,'10-OCT-2012',3009,5002)

into orders(o_no,p_amt,o_date,customer_id,salesman_id) values(70011,75.29,'17-AUG-2012',3003,5007)

into orders(o_no,p_amt,o_date,customer_id,salesman_id) values(70013,045.6,'25-APR-2012',3002,5001)

select 1 from dual;


write a SQL query to find the salespersons and customers who live in same city. Return customer name,
salesperson name and salesperson city

select cust_name, name, b.city

from customer a, salesman b

where a.salesman_id=b.salesman_id

and a.city=b.city;

write a SQL query to find all the customers along with the salesperson who works for them. Return
customer name, and salesperson name.

select cust_name, name

from customer A, salesman B

where A.salesman_id=B.salesman_id;

write a SQL query to find those sales people who generated orders for their customers but not located in the
same city. Return ord_no, cust_name, customer_id (orders table), salesman_id (orders table).

SELECT C.cust_name, O.salesman_id, O.order_no, O.customer_id

FROM customer C, salesman S, orders O

WHERE S.salesman_id = O.salesman_id

AND O.customer_id = C.customer_id

AND S.city <> C.city;

write a SQL query to find those orders made by customers

select * from orders a, customer b where a.customer_id=b.customer_id;

write a SQL query to find the salesperson and customer who belongs to same city. Return Salesman,
cust_name and city.

select c.cust_name, s.name, c.city

from customer c, salesman s

where c.city=s.city;

write a SQL query to find those orders where order amount exists between 500 and 2000. Return ord_no,
purch_amt, cust_name, city.

select o_no, p_amt, cust_name, city from customer a, orders b where a.customer_id=b.customer_id

and p_amt >=500 and p_amt <=2000;


write a SQL query to find the salesperson(s) and the customer(s) he handle. Return Customer Name, city,
Salesman, commission.

select cust_name, a.city, name, commission from customer a, salesman b where a.salesman_id=b.sa

lesman_id;

SELECT a.cust_name AS "Customer Name",

a.city, b.name AS "Salesman", b.commission

FROM customer a

INNER JOIN salesman b

ON a.salesman_id=b.salesman_id;

write a SQL query to find those salespersons who received a commission from the company more than 12%.
Return Customer Name, customer city, Salesman, commission.

select cust_name, name, b.city, commission from customer b inner join salesman a on
a.salesman_id=b.salesman_id and commission > 0.12;

write a SQL query to find all the orders issued by the salesman 'Paul Adam'. Return ord_no, purch_amt,
ord_date, customer_id and salesman_id.

select o_no, p_amt, o_date, customer_id, salesman_id from orders where salesman_id=(select salesman_id
from salesman where name=’Paul Adam’;

write a SQL query to find all the orders, which are generated by those salespeople, who live in the city of
London.Return ord_no, purch_amt, ord_date, customer_id, salesman_id.

select o_no, p_amt, o_date, customer_id, salesman_id from orders where salesman_id=(select salesman_id
from salesman where city=’London’);

write a SQL query to find the orders generated by the salespeople who may work for customers whose id is
3007. Return ord_no, purch_amt, ord_date, customer_id, salesman_id.

select o_no,p_amt,o_date, customer_id, salesman_id from orders where salesman_id=(select customer_id


from orders where customer_id=3007);

write a SQL query to find the order values greater than the average order value of 10th October 2012.
Return ord_no, purch_amt, ord_date, customer_id, salesman_id.

select o_no, p_amt,o_date, customer_id, salesman_id from orders where p_amt>(select avg(p_amt) from
orders where o_date=’10-OCT-2012’);
write a SQL query to find all the orders generated in New York city. Return ord_no, purch_amt, ord_date,
customer_id and salesman_id.

Select * from orders where salesman_id=(select salesman_id from salesman where city=’New York)

write a SQL query to find all the orders generated in New York city or Paris. Return ord_no, purch_amt,
ord_date, customer_id and salesman_id.

Select * from orders where salesman_id=(select salesman_id from salesman where city=’New York’ or
city=’Paris’);
FOE VIEW CREATION

SYNTAX-

create a view for all sales person who are living in paris

create view salesman as select * from salesman where city=’Paris’;

You might also like