Simple and Complex Queries
Simple and Complex Queries
stud_id INT,
name varchar(20),
Primary key(stud_id)
);
livesql.oracle.com
CREATE TABLE Employee (
FName VARCHAR(12),
Minit VARCHAR(12),
LName VARCHAR(12),
SSN char(9),
Super_ssn char(9),
Bdate char(10),
Salary numeric(7,2),
FName VARCHAR(12),
Minit VARCHAR(12),
LName VARCHAR(12),
Super_ssn char(9),
Bdate char(10),
Salary numeric(7,2),
MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9),
UNIQUE (DNAME),
CONSTRAINT fk_dept
REFERENCES Employee(SSN));
UPDATE Employee
WHERE SSN=123456789;
set operation
VALUES (1,'abhi');
VALUES (2,'sid');
VALUES (2,'sid');
VALUES (3,'manu');
Aggregate fuction
SELECT COUNT(*)
FROM Employee;
Emp_name VARCHAR(12),
Job_name VARCHAR(12),
Manager_id INTEGER,
Hiredate char(10),
Salary numeric(7,2),
commision numeric(7,2),
dep_id INTEGER,
CONSTRAINT fk_dept
REFERENCES DEPT(dep_id));
FName VARCHAR(12),
Minit VARCHAR(12),
LName VARCHAR(12),
Super_ssn char(9),
Bdate char(10),
Salary numeric(7,2),
dep_location VARCHAR(10));
Simple queries
https://www.w3resource.com/sql-exercises/employee-database-
exercise/index.php?passed=passed
group by and having
Write a query in SQL to find the highest salary from all the
employees.
SELECT max(salary)
FROM employees;
Write a query in SQL to list the employee id, name, salary, and
department id of the employees in ascending order of salary
who works in the department 1001.
SELECT e.emp_id,
e.emp_name,
e.salary,
e.dep_id
FROM employees E
WHERE e.dep_id = 1001
ORDER BY e.salary ASC;
Write a query in SQL to list the employees who are senior to
their own MANAGERS.
SELECT *
FROM employees w,
employees m
WHERE w.manager_id = m.emp_id
AND w.hire_date < m.hire_date;
Write a query in SQL to list the employees who are drawing the
salary less than 1000 and sort the output in ascending order on
salary.
SELECT *
FROM employees
WHERE salary < 1000
ORDER BY salary;
SELECT *
FROM employees e,
salary_grade s
WHERE e.salary BETWEEN s.min_sal AND s.max_sal
AND s.grade IN (4,
5)
AND e.emp_id IN
(SELECT e.emp_id
FROM employees e
WHERE e.job_name IN ('MANAGER',
'ANALYST'));
SELECT *
FROM employees
WHERE salary >
(SELECT salary
FROM employees
WHERE emp_name = 'JONAS');
Write a query in SQL to list the employees who works in the
same designation as FRANK.
SELECT *
FROM employees
WHERE job_name =
(SELECT job_name
FROM employees
WHERE emp_name = 'FRANK');
SELECT *
FROM employees
WHERE salary IN
(SELECT salary
FROM employees e
WHERE (emp_name = 'FRANK'
OR emp_name = 'BLAZE')
AND employees.emp_id <> e.emp_id)
ORDER BY salary DESC;
Write a query in SQL to list the managers who are not working
under the PRESIDENT.
SELECT *
FROM employees
WHERE emp_id IN
(SELECT manager_id
FROM employees)
AND manager_id NOT IN
(SELECT emp_id
FROM employees
WHERE job_name = 'PRESIDENT');
Write a query in SQL to list the name of the employees who are
getting the highest salary of each department.
SELECT e.emp_name,
e.dep_id
FROM employees e
WHERE e.salary IN
(SELECT max(salary)
FROM employees
GROUP BY dep_id) ;
Complex queries
SELECT e.emp_name,
e.salary,
e.commission
FROM employees e
WHERE
(SELECT max(salary+commission)
FROM employees) >= ANY
(SELECT salary
FROM employees);
Procedures
exec p1(5,4);
select * from emo;
function
cursor
DECLARE
total_rows number(2);
BEGIN
UPDATE customer
SET salary = salary + 500;
IF sql%notfound THEN
dbms_output.put_line('no customers selected');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers selected ');
END IF;
END;
DECLARE
c_id customer.id%type;
c_name customer.name%type;
c_addr customer.address%type;
CURSOR c_customer is
SELECT id, name, address FROM customer;
BEGIN
OPEN c_customer;
LOOP
FETCH c_customer into c_id, c_name, c_addr;
EXIT WHEN c_customer%notfound;
dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr);
END LOOP;
CLOSE c_customer;
END;
https://www.wiseowl.co.uk/sql/exercises/standard/stored-procedures/
https://www.sqlshack.com/sql-server-stored-procedures-for-beginners/
https://data-flair.training/blogs/stored-procedure-in-sql/
https://www.datacamp.com/community/tutorials/introduction-indexing-
sql?
utm_source=adwords_ppc&utm_campaignid=1455363063&utm_adgroupid=65083
631748&utm_device=c&utm_keyword=&utm_matchtype=b&utm_network=g&utm_a
dpostion=&utm_creative=332602034364&utm_targetid=dsa-
429603003980&utm_loc_interest_ms=&utm_loc_physical_ms=9302184&gclid=
CjwKCAiAnIT9BRAmEiwANaoE1RA8sjbsSXVCO4lNutpvUrvdxkDBLW5HF3OgjsvLnufp
sJVvliypthoCgHgQAvD_BwE
indexing
https://www.youtube.com/watch?v=NgYiO6vu1zk
lab on procedure