0% found this document useful (0 votes)
21 views7 pages

Answer Key For SET-1 TO 3

The document contains answer keys for three sets of questions related to Python DataFrame operations and SQL queries. It includes code snippets for filtering data, adding columns, renaming indices, creating bar charts, and various SQL commands for database creation, record insertion, and data retrieval. Each set addresses different datasets and operations, demonstrating practical applications of Python and SQL.

Uploaded by

oppso7136
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)
21 views7 pages

Answer Key For SET-1 TO 3

The document contains answer keys for three sets of questions related to Python DataFrame operations and SQL queries. It includes code snippets for filtering data, adding columns, renaming indices, creating bar charts, and various SQL commands for database creation, record insertion, and data retrieval. Each set addresses different datasets and operations, demonstrating practical applications of Python and SQL.

Uploaded by

oppso7136
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/ 7

Answer Key for SET-1

Question 1: Pandas (Python DataFrame)


(a) Display the details of all the cricketers whose salary is above 4000000.

import pandas as pd

# Creating the DataFrame


data = {
'Cno': [101, 102, 103, 104],
'Cname': ['Rohit', 'Virat', 'Surya', 'Rahul'],
'Cage': [34, 33, 32, 28],
'Salary': [5000000, 9000000, 7000000, 4000000]
}

cricketers = pd.DataFrame(data)

# Filtering cricketers with salary above 4000000


high_salary = cricketers[cricketers['Salary'] > 4000000]

# Displaying the result


print(high_salary)

(b) Add a column named "allowance" which is 30% of salary.

cricketers['Allowance'] = cricketers['Salary'] * 0.30


print(cricketers)

(c) Rename the row index by row1, row2, row3, row4.

cricketers.index = ['row1', 'row2', 'row3', 'row4']


print(cricketers)

(d) Create a bar chart between cricketer’s name and their salaries.

import matplotlib.pyplot as plt

# Plotting the bar chart


plt.bar(cricketers['Cname'], cricketers['Salary'], color='blue')

# Adding labels and title


plt.xlabel("Cricketer's Name")
plt.ylabel("Salary")
plt.title("Cricketers and their Salaries")

# Saving the chart as a JPG file


plt.savefig("cricketers.jpg")

# Displaying the chart


plt.show()

Question 2: SQL Queries


(a) Create a database named Board2023 and create the employees table.
CREATE DATABASE Board2023;

USE Board2023;

CREATE TABLE employees (


Sno INT PRIMARY KEY,
Name VARCHAR(50),
Age INT,
Department VARCHAR(50),
City VARCHAR(50)
);

(b) Write SQL commands to insert the records in the employees table.
INSERT INTO employees VALUES (1, 'Ram', 30, 'Manager', 'Delhi');
INSERT INTO employees VALUES (2, 'Karan', 19, 'Sales', 'Chandigarh');
INSERT INTO employees VALUES (3, 'Shyam', 30, 'Sales', 'Delhi');
INSERT INTO employees VALUES (4, 'Anu', 38, 'Manager', 'Goa');
INSERT INTO employees VALUES (5, 'Rahul', 24, 'Office', 'Goa');

(c) Write a query to display the youngest and oldest age of employees.
SELECT MIN(Age) AS Youngest, MAX(Age) AS Oldest FROM employees;

(d) Display name, age, and department of all the employees of Delhi, arranged in
ascending order of age.
SELECT Name, Age, Department FROM employees
WHERE City = 'Delhi'
ORDER BY Age ASC;

(e) Display the minimum age of all employees of each city.


SELECT City, MIN(Age) AS Minimum_Age
FROM employees
GROUP BY City;

(f) Display the records of all employees whose age is above 25 OR department is
‘Sales’.
SELECT * FROM employees
WHERE Age > 25 OR Department = 'Sales';

(g) Display the total number of employees in each department.


SELECT Department, COUNT(*) AS Total_Employees
FROM employees
GROUP BY Department;

Answer Key for SET-2


1. Python Code (DataFrame Operations)
import pandas as pd
import matplotlib.pyplot as plt

# Creating DataFrame
players = pd.DataFrame({
"Pno": [201, 202, 203, 204],
"Pname": ["David", "Steve", "Chris", "Mark"],
"Page": [29, 28, 30, 27],
"Income": [4500000, 6000000, 3200000, 3800000]
})

# (a) Display details of players with income above 4000000


print(players[players["Income"] > 4000000])

# (b) Add a column "bonus" (25% of income)


players["Bonus"] = players["Income"] * 0.25

# (c) Rename row index


players.index = ["row1", "row2", "row3", "row4"]

# (d) Create bar chart


plt.bar(players["Pname"], players["Income"], color='green')
plt.xlabel("Players")
plt.ylabel("Income")
plt.title("Players' Income")
plt.savefig("players.jpg")
plt.show()

2. SQL Queries (Table: staff)

(a) Create Database and Table

CREATE DATABASE Exam2025;


USE Exam2025;
CREATE TABLE staff (
Sno INT PRIMARY KEY,
Name VARCHAR(50),
Age INT,
Role VARCHAR(50),
City VARCHAR(50)
);

(b) Insert Records

INSERT INTO staff VALUES (1, 'Amit', 31, 'Manager', 'Pune'),


(2, 'Riya', 22, 'HR', 'Delhi'),
(3, 'Raj', 29, 'Sales', 'Mumbai'),
(4, 'John', 35, 'Manager', 'Chennai'),
(5, 'Tina', 26, 'Office', 'Kolkata');

(c) Youngest and Oldest Age

SELECT MIN(Age) AS Youngest, MAX(Age) AS Oldest FROM staff;

(d) Display Mumbai Staff in Ascending Order of Age

SELECT Name, Age, Role FROM staff WHERE City = 'Mumbai' ORDER BY Age ASC;

(e) Minimum Age in Each City

SELECT City, MIN(Age) FROM staff GROUP BY City;

(f) Staff with Age > 25 or Role is Sales

SELECT * FROM staff WHERE Age > 25 OR Role = 'Sales';

(g) Count of Staff in Each Role

SELECT Role, COUNT(*) AS Total FROM staff GROUP BY Role;


Answer Key for SET-3
1. Python Code (DataFrame Operations)
python
CopyEdit
import pandas as pd
import matplotlib.pyplot as plt

# Creating DataFrame
employees = pd.DataFrame({
"EmpID": [301, 302, 303, 304],
"EmpName": ["Aryan", "Neha", "Kabir", "Sonal"],
"EmpAge": [35, 29, 32, 26],
"Salary": [5500000, 4800000, 6200000, 3500000]
})

# (a) Display details of employees with salary above 4500000


print(employees[employees["Salary"] > 4500000])

# (b) Add a column "incentive" (20% of salary)


employees["Incentive"] = employees["Salary"] * 0.20

# (c) Rename row index


employees.index = ["row1", "row2", "row3", "row4"]

# (d) Create bar chart


plt.bar(employees["EmpName"], employees["Salary"], color='red')
plt.xlabel("Employees")
plt.ylabel("Salary")
plt.title("Employees' Salary")
plt.savefig("employees.jpg")
plt.show()
2. SQL Queries (Table: workers)

(a) Create Database and Table

sql
CopyEdit
CREATE DATABASE Test2024;
USE Test2024;
CREATE TABLE workers (
Sno INT PRIMARY KEY,
Name VARCHAR(50),
Age INT,
Position VARCHAR(50),
Location VARCHAR(50)
);

(b) Insert Records

sql
CopyEdit
INSERT INTO workers VALUES (1, 'Arjun', 34, 'Supervisor', 'Bengaluru'),
(2, 'Meera', 25, 'HR', 'Jaipur'),
(3, 'Nikhil', 29, 'Sales', 'Hyderabad');

(c) Youngest and Oldest Age

sql
CopyEdit
SELECT MIN(Age) AS Youngest, MAX(Age) AS Oldest FROM workers;

(d) Display Hyderabad Workers in Ascending Order of Age

sql
CopyEdit
SELECT Name, Age, Position FROM workers WHERE Location = 'Hyderabad' ORDER BY
Age ASC;

(e) Minimum Age in Each Location

sql
CopyEdit
SELECT Location, MIN(Age) FROM workers GROUP BY Location;

(f) Workers with Age > 28 or Position is Sales

sql
CopyEdit
SELECT * FROM workers WHERE Age > 28 OR Position = 'Sales';
(g) Count of Workers in Each Position

sql
CopyEdit
SELECT Position, COUNT(*) AS Total FROM workers GROUP BY Position;

You might also like