You are on page 1of 3

DROP TABLE stores;

CREATE TABLE stores (store_loc VARCHAR2 (30), sales NUMBER, trx_date DATE);
INSERT INTO stores VALUES ('Delhi',1000,SYSDATE-30);
INSERT INTO stores VALUES ('Jammu',3000,SYSDATE-14);
INSERT INTO stores VALUES ('Indore',2000,SYSDATE-2);
INSERT INTO stores VALUES ('Indore',12000,SYSDATE-20);
INSERT INTO stores VALUES ('Chennai',1250,SYSDATE-23);
INSERT INTO stores VALUES ('Chennai',50,SYSDATE-7);
INSERT INTO stores VALUES ('Chennai',150,SYSDATE-9);
INSERT INTO stores VALUES ('Chennai',1150,SYSDATE-3);
INSERT INTO stores VALUES ('Chennai',750,SYSDATE-13);
INSERT INTO stores VALUES ('Hyderabad',750,SYSDATE-3);
INSERT INTO stores VALUES ('Vadodara',3500,SYSDATE-10);
DROP TABLE geography;
CREATE TABLE geography (region VARCHAR2 (10), store_loc VARCHAR2 (30));
INSERT INTO geography VALUES ('North','Delhi');
INSERT INTO geography VALUES ('North','Jammu');
INSERT INTO geography VALUES ('West','Indore');
INSERT INTO geography VALUES ('West','Bombay');
INSERT INTO geography VALUES ('West','Pune');
INSERT INTO geography VALUES ('West','Vadodara');
INSERT INTO geography VALUES ('South','Hyderabad');
INSERT INTO geography VALUES ('South','Chennai');
INSERT INTO geography VALUES ('East','Siliguri');
INSERT INTO geography VALUES ('East','Warangal');
SELECT * FROM stores;
SELECT * FROM geography ORDER BY region;
-- Question: Calculate the total sales of "West" region?
-- Solution:
-- Option# 1
SELECT SUM (sales)
FROM stores s
WHERE EXISTS (SELECT 1
FROM geography g
WHERE region = 'West' AND s.store_loc = g.store_loc);
-- Option# 2
SELECT SUM (sales)
FROM stores s,
geography g
WHERE s.store_loc = g.store_loc
AND g.region = 'South';
-- Option# 3
SELECT SUM (sales)
FROM stores s
WHERE store_loc IN (SELECT store_loc
FROM geography g
WHERE region = 'West');
-- Question: Which location has maximum sales?
-- Solution:
SELECT store_loc, SUM (sales)
FROM stores
GROUP BY store_loc
HAVING SUM (sales) = ( SELECT MAX (SUM (sales)) max_sales
FROM stores
GROUP BY store_loc)
-- Question: What is the maximum sales across all locations?
-- Solution:
SELECT
MAX ( SUM ( sales ) ) max_sales
--SUM ( sales ) , store_loc
FROM stores
GROUP BY store_loc
-- Question: Calculate total sales for each location?
-- Solution:
SELECT
SUM ( sales ) , store_loc
FROM stores
GROUP BY store_loc
-- Question: Which location has maximum sales transactions
-- Solution:
SELECT count (sales), store_loc
FROM stores
GROUP BY store_loc HAVING count (sales) = (SELECT MAX(COUNT (sales)) FROM stores
GROUP BY store_loc)
ORDER BY count (sales) desc
-- Question: Which Region has maximum sales transactions
-- Solution:
SELECT r.region, SUM (s.sales)
FROM stores s, geography r
WHERE s.store_loc = r.store_loc
GROUP BY r.region
HAVING SUM (s.sales) = ( SELECT MAX (SUM (ss.sales)) max_sales
FROM stores ss, geography rr
WHERE ss.store_loc = rr.store_loc
GROUP BY rr.region)
-- Find out the last day of transaction
SELECT MAX(trx_date) FROM stores;
-- Find out the difference between the first and last transactions
SELECT MAX (trx_date) - MIN (trx_date) FROM stores;

select extract(day from sysdate - to_date('2009-10-01', 'yyyy-mm-dd')) from dua


l

ORACLE SQL / PLSQL (IN ANY ALL statements)


------------------------------------------

Find the employees who earn the same salary as the minimum salary for each depar
tment-
SELECT last_name, salary,department_id
FROM employees
WHERE salary IN (SELECT MIN(salary)
FROM employees
GROUP BY department_id);
Employees who are not IT Programmers and whose salary is less than that of any I
T programmer-
SELECT employee_id, last_name, salary, job_id
FROM employees
WHERE salary <ANY
(SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
Employees whose salary is less than the salary ofall employees with a job ID of
IT_PROG and whose job is not IT_PROG-
SELECT employee_id,last_name, salary,job_id
FROM employees
WHERE salary <ALL
(SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG;

You might also like