0% found this document useful (0 votes)
33 views14 pages

Aim: To Solve Queries Using The Concept of Sub Query.: Practical: 7

The document contains a series of SQL queries aimed at solving various database-related tasks, such as retrieving employee and customer information based on specific conditions, updating deposit amounts, and deleting records. Each query is accompanied by a brief description of its purpose and the expected output. The queries utilize concepts such as subqueries, joins, and aggregate functions to manipulate and retrieve data from a relational database.

Uploaded by

ukaniprince215
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views14 pages

Aim: To Solve Queries Using The Concept of Sub Query.: Practical: 7

The document contains a series of SQL queries aimed at solving various database-related tasks, such as retrieving employee and customer information based on specific conditions, updating deposit amounts, and deleting records. Each query is accompanied by a brief description of its purpose and the expected output. The queries utilize concepts such as subqueries, joins, and aggregate functions to manipulate and retrieve data from a relational database.

Uploaded by

ukaniprince215
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

CHOSALIYA MIT 230280116021

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";
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
CHOSALIYA MIT 230280116021

CUSTOMERS.CITY FROM CUSTOMERS WHERE CUSTOMERS.CNAME =”ANIL”);

Output –

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 –
8. List the name of branch having highest number of depositors.
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
CHOSALIYA MIT 230280116021

GROUP BY CITY HAVING COUNT (CNAME) >= ALL(SELECT COUNT(CNAME) FROM


CUSTOMERS GROUP BY CITY));

Output –

*********
CHOSALIYA MIT 230280116021

Practical :8
Aim : To apply the concept of Manipulating Data.
1. Give 10% interest to all depositors.

Query – UPDATE DEPOSITE SET AMOUNT = 1.10 * AMOUNT;

Output –

2. Give 10% interest to all depositors having branch vrce.

Query – UPDATE DEPOSITE SET AMOUNT = 1.10 * AMOUNT WHERE BNAME =‘VRCE’;
CHOSALIYA MIT 230280116021
Output –

3. Give 10% interest to all depositors living in nagpur and having branch city bombay.

Query – UPDATE DEPOSITE SET AMOUNT = 1.10 * AMOUNT WHERE CNAME IN


(SELECT CNAME FROM CUSTOMERS WHERE CCITY = ‘NAGPUR’) AND BNAME IN (SELECT
BNAME FROM BRANCH
WHERE BCITY = ‘BOMBAY’);

Output –

0 row(s) affected Rows matched: 0 Changed: 0 Warnings: 0

4. Write a query which changes the department number of all employees with empno 7788’s job to
employee 7844’s current department number.

Query – UPDATE EMPLOYEE SET DEPT_ID= (SELECT DEPT_ID FROM (SELECT DEPT_ID
FROM
EMPLOYEE WHERE EMP_ID = 104) AS A) WHERE JOB_ID = (SELECT JOB_ID FROM
(SELECT JOB_ID FROM EMPLOYEE WHERE EMP_ID = 108) AS A);
CHOSALIYA MIT 230280116021

Output -
5. Transfer 10 Rs from account of anil to sunil if both are having same branch.
Query –

UPDATE DEPOSIT SET AMOUNT = AMOUNT – 10 WHERE CNAME = ‘ANIL’ AND BNAME =
(SELECT BNAME WHERE CNAME = ‘SUNIL’);

UPDATE DEPOSITE SET AMOUNT = AMOUNT + 10 WHERE CNAME = ‘SUNIL’ AND BNAME
=
(SELECT BNAME WHERE CNAME = ‘ANIL’);

Output -

6. Give 100 Rs more to all depositors if they are maximum depositors in their respective branch.

Query – UPDATE DEPOSITE SET AMOUNT = AMOUNT + 100 WHERE CNAME IN (SELECT
CNAME
FROM (SELECT CNAME FROM DEPOSITE WHERE (BNAME, AMOUNT) IN (SELECT BNAME,
MAX(AMOUNT) FROM DEPOSITE GROUP BY BNAME )) AS A);
Output -
CHOSALIYA MIT 230280116021
7. Delete depositors of branches having number of customers between 1 to 3.

Query – DELETE FROM DEPOSITE WHERE BNAME IN (SELECT BNAME FROM (SELECT
BNAME
FROM DEPOSITE GROUP BY BNAME HAVING COUNT (CNAME) BETWEEN 1 AND 3)
AS A);

Output –

8. Delete deposit of vijay.

Query – DELETE FROM DEPOSITE WHERE CNAME = ‘VIJAY’;

Output –

9. Delete borrower of branches having average loan less than 10

Query – DELETE FROM BORROW WHERE BNAME IN (SELECT BNAME FROM (SELECT BNAME
FROM BORROW GROUP BY BNAME HAVING AVG (AMOUNT) < 1000) AS A)

Output –

*********

You might also like