0% found this document useful (0 votes)
29 views39 pages

Final DBMS Assignment 10 Jun 2024

Uploaded by

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

Final DBMS Assignment 10 Jun 2024

Uploaded by

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

Final DBMS Assignment

DATE - 10/6/2024
Question :
1. Create 'classwork' database. Import classwork-db.sql.

Solution :
Steps to import database file(.sql)

1. Open Workbench
2. Run following queries:
show databases;
create database classwork;
use classwork;
3. Go to databases and click on import tab
4. Select option Individual Database file for import and browse the
classwork-db.sql
5. After selecting database file Click on Start Import button.
6. You will get following screen
Error appeared
ERROR 1046 (3D000) at line 1: No database selected WHILE
database import
To resolve this error edited the given “classwork-db.sql”
Added following sql statement at the starting of file.
USE classwork;

Now database imported successfully,


After successful import of the sql file.
Now check with the left panel following tables has appeared
Question :
2. Create 'hr' database. Import hr-db.sql.

Solution :
Steps to import database file(.sql)

1. Open Workbench
2. Run following queries:
show databases;
create database hr;
use hr;
3. Go to databases and click on import tab
4. Select option Individual Database file for import and browse the
hr-db.sql
5. After selecting database file Click on Start Import button.
6. Error appeared
7. ERROR 1046 (3D000) at line 1: No database selected WHILE
database import
8. To resolve this error edited the given “hr-db.sql”
9. Added following sql statement at the starting of file.
USE hr;

10. Now database imported successfully,


After successful import of the sql file.
Now check with the left panel following tables has appeared
Question :

3. Create 'northwind' database. Import northwind-db.sql.

Solution :

Steps to import database file(.sql)

1. Open Workbench
2. Run following queries:
show databases;
create database northwind;
use northwind;
3. Go to databases and click on import tab
4. Select option Individual Database file for import and browse the
northwind-db.sql
5. After selecting database file Click on Start Import button.
6. Error appeared
7. ERROR 1046 (3D000) at line 1: No database selected WHILE
database import
8. To resolve this error edited the given “northwind-db.sql”
9. Added following sql statement at the starting of file.
USE northwind;

10. Now database imported successfully,


After successful import of the sql file.
Now check with the left panel following tables has appeared
Question :

4. List all tables in classwork database. Display contents of all tables


(one by one).

Solution :
Display all tables from classwork database :

Display contentss of bonus table:


Display contents of books table:
Display contents of dept table:

Display contents of dummy table:


Display contents of emp table:

Display contents of salgrade table:


Question :

5. Login with your user name (d1_12345). List all tables in sales
database. Display contents of all tables (one by one). Answer the
following questions (by observations only – not queries).
a. How many orders data is present in the database?
b. How many customers are present in the database?
c. How many salespeople are present in the database?
d. On which date order with highest amount is placed?
e. For which order salesman got maximum commission? Hint:
you need to refer data of two tables.
f. Which salesman are handling more than two customers? Hint:
you need to refer data of two tables.
g. Which city have single customer?
h. Which city have multiple salespeople?
Solution :
Display all tables from sales database :
Display contents of customers table:

Display contents of orders table:

Display contents of salespeople table:


Answer the following questions (by observations only – not queries).
a. How many orders data is present in the database?
Ans: 10 orders
b. How many customers are present in the database?
Ans: 07 customers

c. How many salespeople are present in the database?


Ans: 05 salespeople

d. On which date order with highest amount is placed?


Ans: 1990-10-04

e. For which order salesman got maximum commission? Hint: you


need to refer data of two tables.
Ans: 3001 and 3006

f. Which salesman are handling more than two customers? Hint: you
need to refer data of two tables.
Ans: There is no salesman in records who handle more than 02
customers.

g. Which city have single customer?


Ans: Berlin

h. Which city have multiple salespeople?


Ans: London
Question :

6. Study relationship between the tables in sales database.

Solution :
One-to-Many Relationship

This is an example of a ‘one-to-many’ type of relationship: one value from


the “cnum” column in the “customers” table can be found many times in the
“cnum” column in the “orders” table. As a relational schema, this is shown
by assigning the correct symbols at the end of the arrow.

The Opposite Direction


Question :

7. Write a select command that produces the order number, amount, and
date for all rows in the Orders table.

Solution :

SQL Statement/Query :

SELECT onum, amt, odate FROM sales.orders;

Output :
Question :

8. Write a query that displays the Salespeople table with the columns in the
following order: city, sname, snum, comm.

Solution :

SQL Statement/Query :

SELECT city,sname,snum,comm FROM sales.salespeople;

Output :
Question :

9. Write a query that will produce the snum values of all salespeople from
the Orders table (with the duplicate values suppressed).

Solution :

SQL Statement/Query :

SELECT distinct(snum) FROM sales.orders;

Output :
Question :

10.Write a query that will give you all orders for more than Rs. 1,000.

Solution :

SQL Statement/Query :

SELECT * FROM sales.orders WHERE amt>1000;

Output :
Question :

11.Write a query that will give you the names and cities of all salespeople in
London with a commission above 0.10.

Solution :

SQL Statement/Query :

SELECT sname,city FROM sales.salespeople WHERE city='London' AND


comm>0.10;

Output :
Question :

12.Write a query on the Customers table whose output will exclude all customers
with a rating <= 100, unless they are located in Rome.

Solution :

SQL Statement/Query :

Method 1 – using NOT keyword

SELECT * FROM sales.customers WHERE NOT rating<=100 OR city


LIKE 'Rome';

Method 2 – without NOT keyword

SELECT * FROM sales.customers WHERE rating>100 OR city LIKE


'Rome';

Output :
Question :

13.Write a query that selects all orders except those with zeroes or NULLs in the
amt field.

Solution :

SQL Statement/Query :

SELECT * FROM sales.orders WHERE (amt IS NOT NULL AND


amt<>0);

Output :

Note: To solve below queries use “hr” database


(HR database data is partial. For few queries you may have empty resultset).
Question :

14. Write a query to display the first_name, last_name using alias name “First
Name", "Last Name".

Solution :

SQL Statement/Query :

Method 1 – using AS keyword

SELECT FIRST_NAME AS "FIRST NAME",LAST_NAME AS "LAST


NAME" FROM employees;

Method 2 – without AS keyword

SELECT FIRST_NAME "FIRST NAME",LAST_NAME "LAST NAME"


FROM employees;

Output :
Question :

15. Write a query to get the names (first_name, last_name), salary, PF of all the
employees (PF is calculated as 15% of salary).

Solution :

SQL Statement/Query :

SELECT FIRST_NAME AS "FIRST NAME",LAST_NAME AS "LAST


NAME", SALARY, SALARY*0.15 AS "PF" FROM employees;

Output :
Question :

16. Write a query to select first 10 records from a employees table.

Solution :

SQL Statement/Query :

SELECT * FROM employees LIMIT 10;

Output :
Question :

17. Write a query to display job id and job title of first 5 jobs.

Solution :

SQL Statement/Query :

SELECT JOB_ID, JOB_TITLE FROM jobs LIMIT 5;

Output :
Note : Question no 18 is missing in assignment
Question :

19. Write a query to display location id, street address and postal code of 6
locations after first 3 locations.

Solution :

SQL Statement/Query :

SELECT LOCATION_ID, STREET_ADDRESS, POSTAL_CODE FROM


locations LIMIT 6 OFFSET 3;

Output :
Question :

20. Write a query to display job title and difference between max and min salary
for that job.

Solution :

SQL Statement/Query :

Method 1 – using distinct( ) keyword

SELECT distinct(JOB_TITLE), MAX_SALARY - MIN_SALARY AS


"SALARY_DIFFERENCE" FROM JOBS;

Method 2 – without AS distinct( )

SELECT distinct(JOB_TITLE), MAX_SALARY - MIN_SALARY AS


"SALARY_DIFFERENCE" FROM JOBS;

Output :
Note: * Use Group by clause with appropriate sql functions to solve
following queries.
Question :

21. Display manager ID and number of employees managed by the manager.

Solution :

SQL Statement/Query :

SELECT MANAGER_ID, count(*) AS EMPLOYEE_COUNT FROM


EMPLOYEES GROUP BY MANAGER_ID;

OR

SELECT MANAGER_ID, count(EMPLOYEE_ID) AS


EMPLOYEE_COUNT FROM EMPLOYEES GROUP BY
MANAGER_ID;

Output :
Question :

22. Display the country ID and number of cities we have in the country.

Solution :

SQL Statement/Query :

SELECT COUNTRY_ID, count(*) AS "CITY_COUNT" FROM


LOCATIONS GROUP BY COUNTRY_ID;

OR

SELECT COUNTRY_ID, count(CITY) AS "CITY_COUNT" FROM


LOCATIONS GROUP BY COUNTRY_ID;

Output :
Question :

23. Display average salary of employees in each department who have commission
percentage.

Solution :

SELECT DEPARTMENT_ID, AVG(SALARY) AS AVG_SALARY FROM


EMPLOYEES WHERE COMMISSION_PCT IS NOT NULL GROUP BY
DEPARTMENT_ID;

Output :
Question :

24. Display job ID, number of employees, sum of salary, and difference between
highest salary and lowest salary of the employees of the job.

Solution :

SQL Statement/Query :

SELECT JOB_ID, count(*) AS "EMPLOYEE_COUNT", sum(SALARY)


AS "SUM_SALARY", MAX(SALARY)-MIN(SALARY) AS
"DIFF_SALARY" FROM EMPLOYEES GROUP BY JOB_ID;

Output :
Question :

25. Display job ID for jobs with average salary more than 10000.

Solution :

SQL Statement/Query :

SELECT JOB_ID, AVG(SALARY) AS "AVG_SALARY" FROM


EMPLOYEES GROUP BY JOB_ID HAVING AVG(SALARY)>10000;

Output :
Question :

26. Display years in which more than 10 employees joined.

Solution :

SQL Statement/Query :

SELECT date_format(HIRE_DATE,'%Y') FROM EMPLOYEES GROUP


BY date_format(HIRE_DATE,'%Y') HAVING count(*)>10;

Output :
JOIN QUERIES
Question :

27. Display department name, manager name, and city.

Solution :

SQL Statement/Query :

SELECT D.DEPARTMENT_NAME, E.FIRST_NAME, L.CITY FROM


DEPARTMENTS D JOIN EMPLOYEES E ON (D.MANAGER_ID =
E.EMPLOYEE_ID) JOIN LOCATIONS L USING(LOCATION_ID);

Output :
Question :

28. Display country name, city, and department name.

Solution :

SQL Statement/Query :

METHOD 01 :

SELECT c.country_name, l.city, d.department_name FROM countries c


JOIN locations l ON (c.country_id = l.country_id) JOIN departments d
USING(location_id);

METHOD 02:

SELECT country_name, city, department_name FROM countries JOIN


locations USING(country_id) JOIN departments USING(location_id);

Output :
Question :

29. Display job title, department name, employee last name, starting date for all
jobs from 1993 to 1998.

Solution :

SQL Statement/Query :

SELECT job_title, department_name, last_name, start_date FROM


job_history JOIN jobs USING (job_id) JOIN departments USING
(department_id) JOIN employees USING (employee_id) WHERE start_date
>= '1993-01-01' AND start_date <= '1998-12-31';

Output :
Question :

30. Display job title and average salary of employees.

Solution :

SQL Statement/Query :

METHOD 01 :

SELECT j.job_title,avg(e.salary) AS avg_salary FROM jobs j JOIN


EMPLOYEES e ON (e.job_ID=j.job_id) GROUP BY j.job_title;

METHOD 02 :

SELECT j.job_title,avg(e.salary) AS avg_salary FROM jobs j JOIN


EMPLOYEES e USING (job_ID) GROUP BY j.job_title;

Output :
Question :

31. Display job title, employee name, and the difference between maximum
salary for the job and salary of the employee.

Solution :

SQL Statement/Query :

METHOD 01 :

SELECT j.job_title,avg(e.salary) AS avg_salary FROM jobs j JOIN


EMPLOYEES e ON (e.job_ID=j.job_id) GROUP BY j.job_title;

METHOD 02 :

SELECT j.job_title,avg(e.salary) AS avg_salary FROM jobs j JOIN


EMPLOYEES e USING (job_ID) GROUP BY j.job_title;

Output :

You might also like