sql-rq

Embed Size (px)

Citation preview

  • 7/28/2019 sql-rq

    1/3

    emp

    eno ename

    Salary

    supno dno

    Write an SQL statement for each of the following requests.

    1. Insert the following record to emp table: 22, "Ahmad", 20000, 2, 3INSERT INTO emp values (22, "Ahmad", 20000, 2, 3);INSERT INTO emp(eno, ename, salary, supno, dno) VALUES (22,"Ahmad", 20000, 2, 3);INSERT INTO emp(ename, eno, salary, supno, dno) VALUES ("Ahamd",22, 20000, 2, 3);

    2. Insert the following record to emp table: 23, "Ali", , 1, 3 (salary is null)

    INSERT INTO emp values (23, "Ali", NULL, 1, 3);INSERT INTO emp values (23, "Ali", , 1, 3);INSERT INTO emp(eno, ename, supno, dno) VALUES (23, "Ali", 1, 3);

    3. List all the data in the emp table. SELECT * FROM emp;

    4. List employee number and salary from emp table.SELECT eno, salary FROM emp;

    5. Change the department of employ # 10 to be 2.UPDATE emp SET dno = 2 WHERE eno = 10;

    6. Change the department of employ # 13 to be 2 and the supervisor to be 3.UPDATE emp SET supno = 3, dno = 2 WHERE eno = 13;

    7. Delete the employee whose name "irwin".DELETE FROM emp WHERE ename = "irwin";

    8. What does the following query do? DELETE FROM emp;

    9. What does the following query do? INSERT INTO proj SELECT * FROM project;10. Retrieve eno, ename, salary for all employees in department 3.

    SELECT eno, ename, salary FROM emp WHERE dno = 3;

    11. Retrieve ename, dno for all employees who has salary greater that 25000.SELECT eno, ename, dno FROM emp WHERE salary > 25000;

    12. Display the salary of each employee with an increase 10%.SELECT eno, ename, salary*1.1 as newsal FROM emp;

    13. Retrieve ename for employees in dept # 2 who has salary less than 250.SELECT ename FROM emp WHERE dno = 2 AND salary < 250;

    14. List all employees that are not in department 1.SELECT * FROM emp WHERE NOT(dno = 1);SELECT * FROM emp WHERE dno 1;

    15. List all employees that has salary between 100 and 250.

    SELECT eno, ename FROM emp WHERE salary>=100 AND salary

  • 7/28/2019 sql-rq

    2/3

    1. List all data about employees. select _______ from _________;

    2. Insert new record to the dept table with the following values: deptno = 30, dname = "housing" and location = "Zalaq".

    insert _________ dept ________ (30, "housing", "Zalaq");

    3. Delete all departments that are located in "Zalaq". delete _ dept _ location="Zalaq";

    4. Give a salary increase by 10% for all employees in department #3.

    ________ emp ________ salary = salary*1.1 where deptno=3;

    5. List empno, ename, and salary for all employees who have commission 10.

    _________ empno, ename, salary from emp where ______________;

    6. List empno and salary for all employees who have salary greater that 2000.

    select empno, salary from __________ where ________________;

    7. List empno and salary of each employee with an increase 10%. Name the increased salary as newsal.

    select empno, salary*_______ as ______ from emp;

    8. List empno, ename for employees in department # 2 who has salary less than 1500.

    select empno, ename from emp where deptno = 2 ______ salary _____;

    9. List all data about employees that are not in department #1.

    select _______ from emp where ________ (deptno = 1);

    10. List all data about employees that have salary between 1000 and 2000.

    select * from emp WHERE salary _________ 1000 _______ 2000;

    11. List all data about employees that do not have a manager.

    select * from emp where ___________ is ___________;

    12. List all data about employees whose names end with the letter "d".

    select * from emp where ename ________"___";

    13. List all data about employees who have commission 10 or 40.

    select * from emp where ________ ________ (10, 40);

    14. List all data about employees sorted by their salaries descending.

    select * from emp _________ salary ________;

    15. List all unique commission values. select ___________ comm ________ emp;

    16. List the maximum salary value. select _______(_________) from emp;

    17. List the average of salary values of employees in department #3.

    select ________(________) from emp where deptno = 3;

    18. For each department, list deptno and the sum of salary values of its employees.

    select deptno, sum(salary) from emp ____________ _______;

    19. List the deptno for departments that have more than 3 employees.

    select deptno from emp group by deptno _______ _______ > 3;

    20. What is the output of the following query?

    select * from emp where comm < (select min(comm) from emp);

  • 7/28/2019 sql-rq

    3/3

    Consider the following table descriptions. Solve in SQL the following queries.CUSTOMERS A table containing information about customers

    cid Unique identifier for a customer

    cname Name of a customer

    city City where the customer is located

    discnt Each customer has a discount percent

    AGENTS A table containing information about agent employeesaid Unique identifier for an agent

    aname Name of agent

    city City where agent is basedpercent Percent commission each agent receives on each sale

    PRODUCTS A table containing information about products for salepid Unique identifier for a product

    pname Name of product

    city City where this product is warehoused

    quantity Quantity on hand for sale

    price Wholesale price for each unit product

    ORDERS A table containing information about ordersordno Unique identifier for this order

    month Month the order was placed

    cid This customer

    aid ... purchased through this agent ...pid ... this specific product ...

    qty ... in this total quantity ...

    dollars ... at this dollar cost

    a) Find all (ordno, pid) pairs fororders ofquantity equal to 1000 or more.

    SELECT ordno, pid FROM ORDERS WHERE qty >= 1000;b) Find all product names ofproductspriced between $0.5 and $1.00 inclusive.

    SELECT pname FROM PRODUCTS WHERE price between 0.5 and 1;c) Find all (ordno, cname) pairs fororders ofdollar value less than $500. Use one join here.

    SELECT ordno, cname FROM ORDERS o, CUSTOMERS cWHERE dollars < 500 and o.cid = c.cid;

    d) Find all (ordno, aname) pairs fororders in March month. Use one join here.

    SELECT ordno, aname FROM ORDERS o, AGENTS aWHERE month = March and o.aid = a.aid;

    e) Find all (ordno, cname, aname) triples fororders in March month. Use two joins here.

    SELECT ordno, cname, aname FROM ORDERS o, CUSTOMERS c, AGENTS aWHERE month = March and o.cid = c.cid and o.aid = a.aid;

    f) Find all the names ofagents in Amman who placed orders with value less than $500.

    SELECT a.aname FROM ORDERS o, AGENTS aWHERE a.city = Amman and o.dollars < 500 and o.aid = a.aid;

    g) Find all the products names ofproducts in Amman city ordered in March month.

    SELECT p.pname FROM ORDERS o, PRODUCTS pWHERE p.city = Amman and o.month = March and o.pid = p.pid;

    h) Find all (cid, aid, pid) triples forcustomer, agent, productcombinations that are all in the same city.

    SELECT c.cid, a.aid, p.pid FROM CUSTOMERS c, AGENTS a, PRODUCTS p

    WHERE c.city = a.city and a.city = p.city;i) Display all pairs of aids foragentwho live in the samecity.

    SELECT a1.aid, a2.aid FROM AGENTS a1, AGENTS a2WHERE a1.city = a2.city and a1.aid < a2.aid;

    j) Find pids ofproductordered through agent a03 but not through agent a06.

    SELECT Distinct pid FROM ORDERSWHERE aid = a03 and pid not in (Select pid from orders where aid = a06)

    k) Get aids ofagents who place individual orders greater than $500 for customers living in Amman city.

    SELECT aid FROM CUSTOMERS c, ORDERS oWHERE o.dollars > 500 and c.city = Amman and c.cid = o.cid;