0% found this document useful (0 votes)
11 views24 pages

Avi Dbhup

Uploaded by

vsenterprise2917
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)
11 views24 pages

Avi Dbhup

Uploaded by

vsenterprise2917
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/ 24

LATHIYA AVI A.

202330067 SYBCA-D

Sr. Description Page Sign


No. No.
1. Create the following tables and enter at least
10 records with appropriate constraints:
Employee(employee_id, first_name,
last_name, age, email, phone_number,
hier_date, job_id, salary, commission_pct,
manager_id, department_id)
Departments(Department_id,
Department_name, manager_id, Location_id)
Location(location_id, street_address,
postal_code, city, state_province,
country_name, region_id)
A) Write a query to display the
names(first_name, last_name) using alias
name “First Name”, “Last Name”
B) Write a query to select first 10 records
from a table
C) Write a query to display the last names of
employees whose names have exactly 6
characters
D) Write a query to get the department ID
and the total salary payable in each
department
E) Write a query to find the
names(first_name, last_name) of the
employees who have a manager who
works for a department based in the
united states
F) Write a query to find the
names(first_name, last_name),the salary

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

of the employees who earn more than the


average salary and who works in any of
the IT departments
G) Write a query t find the employee id ,
name(last_name) along with their
manager_id, manager name(last_name).
H) Write a query to display the job title and
average salary of employees:
I) Write a query to find the
addresses(location_id, street_address,
city, state_province, country_name) of all
the departments
2 Write a command to Dump entire datebase
with proper fie name and tables structure into
a file named as your rollno.
3 Write a trigger called AGECHECK on table
employees that don’t allow the insertion or
update of any record that has an age less than
18.
4 Write a python program to find mean median,
mode from set of numbers in a list.
5 Write a python program to retrieve all rows
from employee table and display the column
values in tabular format.
6 Write a python program to read CSV file and
upload data into table.
7 Write a python program to retrieve all rows
from employee table and dump into
‘employee_details.cvs’ CSV file.
8 Write a program to implement DML operation
using sqlite3.

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

9 Get total salary from employee table and show


line plot with the following style properties
Generated line plot must include following
style properties:-
 Line style dotted and line-color should be
red
 Show legend at the lower right location.
 X label name = salary
 Y label name = Employee name
 Add a circle marker.
 Line marker color as read
 Line width should be 3
10 Use employee_details.csv file and read salary
and commission_pct data and show it using the
bar chart
The bar chart should display the number of
units for each employee.
Add a separate bar for each first name in the
same chart.

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

1)

 CREATE TABLE Employee ( employee_id INTEGER


PRIMARY KEY, first_name TEXT, last_name TEXT, age
INTEGER, email TEXT, phone_number TEXT, hire_date
DATE, job_id INTEGER, salary REAL, commission_pct
REAL, manager_id INTEGER, department_id INTEGER);

 CREATE TABLE Departments ( department_id INTEGER


PRIMARY KEY, department_name TEXT, manager_id
INTEGER, location_id INTEGER);

 CREATE TABLE Location ( location_id INTEGER PRIMARY


KEY, street_address TEXT, postal_code TEXT, city TEXT,
state_province TEXT, country_name TEXT, region_id
INTEGER );

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

A) Write a query to display the names(first_name,


last_name) using alias name “First Name”, “Last
Name”.

 SELECT first_name “First Name” , last_name “Last


Name” FROM Employee;

B) Write a query to select first 10 records from a table.

 SELECT * FROM Employee;

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

 SELECT * FROM Departments;

 SELECT * FROM Location;

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

C) Write a query to display the last names of employees


whose names have exactly 6 characters.

 select last_name from employee where


length(first_name) = 6;

D) Write a query to get the department ID and the total


salary payable in each department.

 SELECT d.Department_id, SUM(e.Salary) AS


Total_Salary FROM Departments d JOIN Employee
e ON d.Department_id = e.Department_id GROUP
BY d.Department_id;

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

E) Write a query to find the names (first_name, last_name)


of the employees who have a manager who works for a
department based in the united states.

 SELECT e.first_name, e.last_name FROM


Employee e JOIN Employee m ON e.manager_id =
m.employee_id JOIN Departments d ON
m.department_id = d.department_id JOIN
Location l ON d.location_id = l.location_id WHERE
l.country_name = 'United States';

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

F) Write a query to find the names(first_name,


last_name),the salary of the employees who earn more
than the average salary and who works in any of the IT
departments

 SELECT first_name, last_name, Salary FROM


Employee WHERE Salary > ( SELECT AVG(Salary)
FROM Employee ) AND Department_id IN ( SELECT
Department_id FROM Departments WHERE
Department_name = 'IT' );

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

G) Write a query t find the employee id , name(last_name)


along with their manager_id, manager name(last_name).

 SELECT e.Employee_id, e.Last_name,


e.Manager_id, m.Last_name AS
Manager_Last_name FROM Employee e JOIN
Employee m ON e.Manager_id = m.Employee_id;

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

H) Write a query to display the job title and average salary of


employees.

 SELECT Job_id, AVG(Salary) AS Average_Salary


FROM Employee GROUP BY Job_id;

I) Write a query to find the addresses(location_id,


street_address, city, state_province, country_name) of all
the departments.

 SELECT d.Location_id, l.Street_address, l.City,


l.State_province, l.Country_name FROM
Departments d JOIN Location l ON d.Location_id =
l.Location_id;

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

2) Write a command to Dump entire datebase with


proper fie name and tables structure into a file named
as your rollno.

 sqlite3 swapnil.db .schema > 81.sql && sqlite3


swapnil.db .dump >> 81.sql

 sqlite3 swapnil.db .schema > 81.sql: This


command retrieves the schema of the database
and writes it to a file named 81.sql. The schema
includes the table definitions and any other
structural information.

 sqlite3 swapnil.db .dump >> 81.sql: This


command dumps the entire database and appends
it to the 81.sql file. The dump includes all the data
and commands needed to recreate the database.

 81.sql file was Created.

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

3) Write a trigger called AGECHECK on table employees


that don’t allow the insertion or update of any record
that has an age less than 18.

 CREATE TRIGGER AGECHECK


BEFORE INSERT ON employee
FOR EACH ROW
WHEN NEW.age < 18
BEGIN
SELECT CASE
WHEN (SELECT 1 FROM employee WHERE id =
NEW.id) IS NULL THEN
RAISE (ABORT, 'Age must be 18 or greater
for new records.')
ELSE
RAISE (ABORT, 'Age must be 18 or greater
for updated records.')
END;
END;

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

4) Write a python program to find mean median,


mode from set of numbers in a list.

 from collections import Counter


import statistics
#list
num_list = [1,4,2,5,7,4,4,7,9,5,8]
#calculation of mean
mean = sum(num_list) / len(num_list)
#calculation of median
median = statistics.median(num_list)
#calculation of mode
mode = statistics.mode(num_list)

print(mean)
print(median)
print(mode)

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

5) Write a python program to retrieve all rows from


employee table and display the column values in
tabular format.

 import sqlite3

def display_employee_table():
# Connect to the database
conn = sqlite3.connect('swapnil.db') # Replace
'your_database.db' with your actual database
name
cursor = conn.cursor()

# Retrieve all rows from the employee table


cursor.execute("SELECT * FROM Employee")
rows = cursor.fetchall()

# Display the column names


column_names = [description[0] for description
in cursor.description]
print("\t".join(column_names))

# Display the rows in tabular format


for row in rows:
print("\t".join(str(value) for value in row))

# Close the database connection


conn.close()
# Call the function to display the employee table
display_employee_table()

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

6) Write a python program to read CSV file and


upload data into table.
 import csv
import sqlite3

# Connect to the SQLite database


conn = sqlite3.connect('swapnil.db')
cursor = conn.cursor()

# Create the table if it doesn't exist


cursor.execute('''
CREATE TABLE IF NOT EXISTS Employee (
employee_id INTEGER PRIMARY KEY,
first_name TEXT,
last_name TEXT,
age INTEGER,
email TEXT,
phone_number TEXT,
hire_date TEXT,
job_id INTEGER,
salary REAL,
commission_pct REAL,

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

manager_id INTEGER,
department_id INTEGER
)
''')

# Read the CSV file and insert records into the table
with open('prog6.csv', 'r') as file:
reader = csv.reader(file)
# next(reader) # Skip the header row if it exists
for row in reader:
cursor.execute('''
INSERT INTO Employee (
employee_id, first_name, last_name, age,
email, phone_number,
hire_date, job_id, salary, commission_pct,
manager_id, department_id
) VALUES (1, 'John', 'Doe', 30,
'[email protected]', '1234567890', '2022-01-
01', 1, 5000, 0.1, NULL, 1)
''', row)

# Commit the changes and close the connection


conn.commit()
conn.close()

print("Data uploaded successfully.")

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

7) Write a python program to retrieve all rows from


employee table and dump into
‘employee_details.cvs’ CSV file.

 import csv
import sqlite3

# Connect to the SQLite database


conn = sqlite3.connect('swapnil.db')
cursor = conn.cursor()

# Retrieve all rows from the Employee table


cursor.execute('SELECT * FROM Employee')
rows = cursor.fetchall()

# Define the CSV file path and header


csv_file = 'employee_details.csv'
csv_header = ['employee_id', 'first_name',
'last_name', 'age', 'email', 'phone_number',
'hire_date', 'job_id', 'salary',
'commission_pct', 'manager_id', 'department_id']

# Write the rows into the CSV file


with open(csv_file, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(csv_header)
writer.writerows(rows)

# Close the connection


conn.close()

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

print(f"Data dumped successfully into '{csv_file}'.")

8) Write a program to implement DML operation


using sqlite3.

 import sqlite3

# Connect to the SQLite database


conn = sqlite3.connect('swapnil.db')
cursor = conn.cursor()

# Create a table
cursor.execute('''CREATE TABLE IF NOT EXISTS
employees
(id INTEGER PRIMARY KEY
AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER NOT NULL,
salary REAL NOT NULL)''')

# Insert a row
cursor.execute("INSERT INTO employees (name,
age, salary) VALUES (?, ?, ?)", ('John Doe', 30,
5000.0))
conn.commit()

# Update a row

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

cursor.execute("UPDATE employees SET salary = ?


WHERE id = ?", (6000.0, 1))
conn.commit()

# Delete a row
cursor.execute("DELETE FROM employees WHERE
id = ?", (1,))
conn.commit()

# Select all rows


cursor.execute("SELECT * FROM employees")
rows = cursor.fetchall()

# Print the selected rows


for row in rows:
print(row)

# Close the database connection


conn.close()

9) Get total salary from employee table and show line plot
with the following style properties
Generated line plot must include following style
properties:-
 style dotted and line-color should be red
 Show legend at the lower right location.

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

 X label name = salary


 Y label name = Employee name
 Add a circle marker.
 Line marker color as read

 import sqlite3
import matplotlib.pyplot as plt

# Connect to the SQLite database


conn = sqlite3.connect('swapnil.db')
cursor = conn.cursor()

# Fetch the total salary for each employee


cursor.execute("SELECT first_name, salary FROM
employee")
rows = cursor.fetchall()

# Extract the employee names and salaries from


the fetched rows
employee_names = [row[0] for row in rows]
salaries = [row[1] for row in rows]

# Close the database connection


conn.close()

# Create the line plot


plt.plot(salaries, employee_names,
linestyle='dotted', color='red', marker='o',
markerfacecolor='red', linewidth=3)

# Set the labels and title

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

plt.xlabel('Salary')
plt.ylabel('Employee Name')
plt.title('Total Salary by Employee')

# Show the legend


plt.legend(['Total Salary'], loc='lower right')

# Display the line plot


plt.show()

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

10) Use employee_details.csv file and read salary and


commission_pct data and show it using the bar chart
The bar chart should display the number of units for each
employee. Add a separate bar for each first name in the
same chart.

 import pandas as pd
import matplotlib.pyplot as plt

# Read the CSV file into a pandas DataFrame


df = pd.read_csv('employee_details.csv')

# Group the data by first name and calculate the


sum of salary and commission_pct for each
employee
grouped_df = df.groupby('first_name').sum()

# Get the first names and their corresponding


salary and commission_pct sums
first_names = grouped_df.index
salary_sums = grouped_df['salary']
commission_sums = grouped_df['commission_pct']

# Generate the bar chart


plt.bar(first_names, salary_sums, label='Salary')
plt.bar(first_names, commission_sums,
label='Commission Pct')

SUB-DBHUP
LATHIYA AVI A. 202330067 SYBCA-D

# Set the labels and title


plt.xlabel('Name of Employees')
plt.ylabel('Number of Units')
plt.title('Salary and Commission Pct by First Name')

# Show the legend


plt.legend()

# Display the bar chart


plt.show()

SUB-DBHUP

You might also like