RONAK
RONAK
2481376
Create the following tables and enter at least 10 records with appropriate
constraints:
1. Employees (employee_id, first name, last_name, age, email, phone number, hire date, job id,
salary, commission pct, manager id, department id)
2. Departments ( Department_Id, Department_ Name, Manager Id, Location Id)
3. Locations ( location id, street address, postal_code city, state_province, country id)
A) Write a query to display the names (first name, last name) using alias name "First
Name", "Last Name")
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 of the employees
who can more than the average salary and who works in any of the IT departments
G) Write a query to 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
Table -1
first_name TEXT,
last_name TEXT,
age INTEGER,
email TEXT,
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
phone_number TEXT,
hire_date TEXT,
job_id TEXT,
salary REAL,
commission_pct REAL,
manager_id INTEGER,
department_id INTEGER );
Table -2
department_name TEXT,
manager_id INTEGER,
location_id INTEGER);
Table -3
street_address TEXT,
postal_code TEXT,
city TEXT,
state_province TEXT,
country_id TEXT);
Table -4
country_name TEXT,
region_id INTEGER);
Table Insert-1
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
Table Insert-2
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
Table Insert-3
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
Table Insert-4
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
Quries:-
A) Write a query to display the names (first name, last name) using alias name "First Name",
"Last Name")
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 de np ta. r tme
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
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 of the employees who can more
than the average salary and who works in any of the IT departments.
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
G) Write a query to 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.
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
PRATICLE-2
Write a command to dump entire database with proper file name and tables
structure into a file named as your rollno.
SOLUTION:
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
stud_name varchar2(30),
stud_salary integer
);
);
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
);
dept_name varchar2(35),
dept_salary numeric
);
10
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
d_name varchar2(30));
ename varchar2(35),
salary number);
11
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
age integer,
mark real);
age integer,
mark real);
first_name text(30),
last_name text(30),
age integer,
email varchar(30),
phone_number integer,
hire_date DATE,
job_id text(30),
salary REAL,
commission_pct REAL,
manager_id integer,
location text(30));
first_name TEXT,
12
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
last_name TEXT,
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,
location TEXT
);
first_name text(30),
last_name text(30),
age integer,
email text(40),
phone_number integer,
hire_date DATE,
job_id integer,
salary REAL,
commision_act real,
manager_id integer,
department_id integer);
13
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
departmen_name text(30),
manager_id integer,
location_id integer);
14
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
country_id integer,
country_name text(40),
region_id integer);
street_address text(40),
postal_code integer,
city text(40),
state_province text(40),
country_id integer);
15
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
age integer,
mark real);
age integer);
COMMIT;
16
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
PRACTILE-3
Write a trigger called AGECHECK on table EMPLOYE that don’t allow the
insertion or update or any record that has an age less than 18.
SOLLUTION:
17
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
PRACTILE-4
Write a python to find mean, median and mode from set of number in a list.
SOLUTTION:
import statistics
def calculate_statistics(numbers):
if not numbers:
mean = statistics.mean(numbers)
median = statistics.median(numbers)
mode = statistics.mode(numbers)
def main():
numbers = [1, 2, 2, 3, 4, 5, 5, 5, 6, 7, 8, 9]
try:
print(f"Mean: {mean}")
print(f"Median: {median}")
print(f"Mode: {mode}")
except ValueError as e:
print(e)
main() OUTPUT:-
18
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
PRACTILE-5
Write a python to retrieve all rows from Employe table and display the column values
in tabular format.
SOLUTION:
import sqlite3
import pandas as
pd def fetch_and_display_employees(db_file):
try:
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
print(df)
except sqlite3.Error as err:
print(f"Error: {err}")
finally:
if conn:
conn.close()
db_file = "C:/sqlite/jinu.db"
fetch_and_display_employees(db_file)
19
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
OUTPUT:
20
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
PRACTILE-6
Write a python program to read CSV file and upload data into table.
SOLUTION:
import sqlite3
import csv
# Function to create a database connection and table
def create_table(database_path):
connection = sqlite3.connect(database_path)
cursor = connection.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS employees2 ( id INTEGER PRIMARY KEY, name TEXT, age INTEGER,
department TEXT ) ''')
# This command creates the table if it does not exist
connection.commit()
connection.close()
# Function to read CSV file and insert data into the table
connection = sqlite3.connect(database_path)
cursor = connection.cursor()
with open(csv_file, 'r') as file:
cursor.execute
connection.commit()
connection.close()
database_path = r'C:\sqlite\vishal.db'
21
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
create_table(database_path)
cursor = connection.cursor()
cursor.execute('SELECT * FROM employees2')
rows = cursor.fetchall()
for row in rows: print(row) #
Print each row to verify insertion connection.close()
22
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
PRACTILE-7
Write a python to retrieve all rows from Employe table and dump into
‘employee_details.csv’ CSV file.
SOLUTION:
import sqlite3
import csv
def export_to_csv(db_path, table_name, csv_file_path):
Args:
"""
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
csv_file_path = 'C:\\sqlite\\employee_details.csv'
export_to_csv(database_path, table_name, csv_file_path)
23
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
24
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
PRACTILE-8
SOLUTION:
INSERT-
UPDATE-
25
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
DELETE-
PRACTILE-9
Get total salary from employe table and show line plot with following style properites
Genertated line plot must include following style properties:-
• Line style dotted and line-color should be red
• Show legend at lower right location
• X label name = salary
• Y label name = employe name
• Add a circle marker
• Line marker color as read
• Line wdth should be 3.
SOLUTION:
import sqlite3
conn = sqlite3.connect('C:\\sqlite\\jinu.db')
cursor = conn.cursor()
cursor.execute("SELECT first_name, salary FROM employees")
data = cursor.fetchall()
conn.close()
# Prepare data for plotting names = [row[0] for row in data] salaries = [row[1] for row in data]
# Create the plot plt.plot(salaries, names, linestyle=':', color='red',
marker='o',markerfacecolor='red', linewidth=3)
# Add labels and legend
26
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
plt.xlabel('Salary')
plt.ylabel('Employee Name')
plt.show()
OUTPUT:
27
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
PRACTILE-10
Use employe_details.csv file and read salary and commission_pct data and show it
using the bar chart should display the number of units for each employe. Add a
separate bar each first name in the same chart.
SOLUTION:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df = pd.read_csv('C:\\sqlite\\employee_details.csv')
df.rename(columns={'commissic': 'commission_pct'}, inplace=True)
# Prepare data for plotting
employees = df['first_name']
salaries = df['salary']
commissions = df['commission_pct']
# Set the positions and width for the bars
x = np.arange(len(employees)) # the label locations
ax1.tick_params(axis='y', labelcolor='blue')
ax2 = ax1.twinx()
28
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
ax1.set_xticks(x)
ax1.set_xticklabels(employees)
ax1.set_xticklabels(employees, fontsize=8)
fig.legend(loc='upper right', bbox_to_anchor=(1,1))
plt.xticks(rotation=45)
plt.show()
OUTPUT:-
29
KANKADIYA RONAK
SY BCA Div:-D DBHUP-journal. 2481376
30
KANKADIYA RONAK