Practical: 7: Aim: To Solve Queries Using The Concept of Sub Query
Practical: 7: Aim: To Solve Queries Using The Concept of Sub Query
Practical: 7
Aim: To solve queries using the concept of sub query.
1. Write a query to display the last name and hire date of any employee in the same
department as AMAN. Exclude AMAN.
Query – SELECT EMP2.HIRE_DATE, EMP2.LNAME FROM EMP2 INNER JOIN
DEPARTMENT
ON EMP2.DEPT_NO = DEPARTMENT.DNO WHERE DEPARTMENT.DNAME = ”SCOTT” ;
Output -
2. Give name of customers who are depositors having same branch city of mr. sunil.
Query – SELECT DEPOSIT.CNAME FROM DEPOSIT INNER JOIN CUSTOMERS ON
DEPOSIT.CNAME = CUSTOMERS.CNAME WHERE DEPOSIT.BNAME = ”VRCE”;
Output –
3. Give deposit details and loan details of customer in same city where pramod is living.
Query –
SELECT LOANNO, BORROW.CNAME,BORROW.AMOUNT AS "B-
AMT",DEPOSIT.ACTNO,BORROW.BNAME, DEPOSIT.AMOUNT AS "D-AMT",ADATE FROM
BORROW INNER JOIN CUSTOMERS ON BORROW.CNAME = CUSTOMERS.CNAME INNER
JOIN
DEPOSIT ON CUSTOMERS.CNAME = DEPOSIT.CNAME WHERE
CUSTOMERS.CITY="NAGPUR";
Kasawala Fenil 230280116056
Output –
4. Create a query to display the employee numbers and last names of all employees who earn
more than the average salary. Sort the results in ascending order of salary.
Query –
SELECT EMP_NO, LNAME FROM EMPLOYEE WHERE EMP_SAL > (SELECT
AVG(EMP_SAL) FROM EMPLOYEE) ORDER BY EMP_SAL;
Output –
5. Give names of depositors having same living city as mr. anil and having deposit amount
greater than 2000.
Query –
SELECT DEPOSIT.CNAME FROM DEPOSIT INNER JOIN CUSTOMERS ON
DEPOSIT.CNAME =
CUSTOMERS.CNAME WHERE DEPSOIT.AMOUNT > 2000 AND CUSTOMERS.CITY =
(SELECT
CUSTOMERS.CITY FROM CUSTOMERS WHERE CUSTOMERS.CNAME =”ANIL”);
Output –
Kasawala Fenil 230280116056
6. Display the last name and salary of every employee who reports to ford.
Query –
SELECT EMPLOYEE.LNAME, EMPLOYEE.EMP_SAL FROM EMPLOYEE INNER JOIN EMP3
ON EMPLOYEE.EMP_NO = EMP3.EMP_ID WHERE EMP3.REP_COMP= "FORD";
Output –
7. Display the department number, name, and job for every employee in the Accounting
department.
Query –
SELECT DEPARTMENT.DNO, DEPARTMENT.DNAME, JOB.JOB_ID FROM
DEPARTMENT INNER
JOIN EMP3 ON EMP3.DEP_NO=DEPARTMENT.DNO INNER JOIN JOB ON JOB.JOB_ID =
EMP3.JOB_ID WHERE JOB.JOB_ID=”FI_ACC”;
Output –
Query –
SELECT D.BNAME FROM DEPOSIT D GROUP BY D.BNAME HAVING
COUNT(D.CNAME) >=
ALL(SELECT COUNT (D1.CNAME) FROM DEPOSIT D1 GROUP BY D1.BNAME) ;
Output –
9. Give the name of cities where in which the maximum numbers of branches are located.
Query –
SELECT B1.CITY FROM BRANCH B1 GROUP BY B1.CITY HAVING
COUNT(B1.BNAME)>=
ALL(SELECT COUNT (B2.BNAME) FROM BRANCH B2 GROUP BY B2.CITY);
Output –
10. Give name of customers living in same city where maximum depositors are located.
Query –
SELECT CNAME FROM CUSTOMERS WHERE CITY LIKE(SELECT CITY FROM
CUSTOMERS
GROUP BY CITY HAVING COUNT (CNAME) >= ALL(SELECT COUNT(CNAME) FROM
CUSTOMERS GROUP BY CITY));
Kasawala Fenil 230280116056
Output –
*********
Kasawala Fenil 230280116056
Kasawala Fenil 230280116056
Practical :8
Output –
Query – UPDATE DEPOSITE SET AMOUNT = 1.10 * AMOUNT WHERE BNAME =‘VRCE’;
Kasawala Fenil 230280116056
Output –
3. Give 10% interest to all depositors living in nagpur and having branch city bombay.
Output –
4. Write a query which changes the department number of all employees with empno 7788’s job
to employee 7844’s current department number.
Output -
5. Transfer 10 Rs from account of anil to sunil if both are having same branch.
Query –
Output -
6. Give 100 Rs more to all depositors if they are maximum depositors in their respective branch.
Output –
Output –
Query – DELETE FROM BORROW WHERE BNAME IN (SELECT BNAME FROM (SELECT BNAME
FROM BORROW GROUP BY BNAME HAVING AVG (AMOUNT) < 1000) AS A)
Output –
*********