0% found this document useful (0 votes)
3 views6 pages

Practical SQL and File Operations Guide

Uploaded by

coloufull.in
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)
3 views6 pages

Practical SQL and File Operations Guide

Uploaded by

coloufull.in
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

Question 1 practical paper set 1

Option 1: File Operations

with open("student_records.txt", "w") as file:


[Link]("Alice | 101 | 85\nBob | 102 | 90\n")

with open("student_records.txt", "r") as file:


print([Link]())

Option 2: MySQL Database Operations

import [Link]

conn = [Link](host="localhost", user="root",


password="password", database="company")
cur = [Link]()

[Link]("CREATE TABLE IF NOT EXISTS employees (emp_id INT PRIMARY


KEY, emp_name VARCHAR(50), emp_salary FLOAT, emp_department
VARCHAR(50))")
[Link]("INSERT INTO employees VALUES (%s, %s, %s, %s)", [
(1, 'Alice', 50000, 'HR'),
(2, 'Bob', 60000, 'Finance'),
(3, 'Charlie', 70000, 'IT')
])
[Link]("SELECT * FROM employees")
for row in [Link]():
print(row)

[Link]()
[Link]()
Question 2 practical paper set 1

• Students in Grade A

sql
Copy code
SELECT * FROM students WHERE Grade = 'A';

• Count Students by Grade

sql
Copy code
SELECT Grade, COUNT(*) FROM students GROUP BY Grade;

• Courses and Instructors

sql
Copy code
SELECT course_name, Instructor FROM courses;

• Add marks Column

sql
Copy code
ALTER TABLE students ADD marks INT;

• Update Grade for Student 104

sql
Copy code
UPDATE students SET Grade = 'B' WHERE student_id = 104;
Question 1 practical paper set 2

Option 1: Binary File Operations

import pickle
with open("employee_data.dat", "wb") as f: [Link]([(101, "John
Doe", 45000.00), (102, "Jane Smith", 50000.00)], f)
with open("employee_data.dat", "rb") as f: print([Link](f))

Option 2: MySQL Database Operations

import [Link]

conn = [Link](host="localhost", user="root", password="password",


database="library")

cur = [Link]()

[Link]("CREATE TABLE IF NOT EXISTS books (id INT, title VARCHAR(100), author
VARCHAR(100), year INT, price FLOAT)")

[Link]("INSERT INTO books VALUES (%s, %s, %s, %s, %s)", [(1, "1984",
"Orwell", 1949, 350), (2, "Mockingbird", "Lee", 1960, 300), (3, "Gatsby", "Fitzgerald", 1925,
400)])

[Link]("SELECT * FROM books")

print([Link]())

[Link]()

[Link]()
Question 2 practical paper set 2

-- 1. Employees with salary > 50000

SELECT emp_name FROM employees WHERE emp_salary > 50000;

-- 2. Average salary

SELECT AVG(emp_salary) FROM employees;

-- 3. Count employees in Sales

SELECT COUNT(*) FROM employees e JOIN departments d ON e.emp_id = d.dept_id


WHERE dept_name = 'Sales';

-- 4. Insert new employee

INSERT INTO employees VALUES (505, 'Kate', 30, 55000);

-- 5. Delete employee with emp_id = 504

DELETE FROM employees WHERE emp_id = 504;


Question 1 practical paper set 3

Option 1: CSV File Operations

import csv
with open("[Link]", "w", newline="") as file:
writer = [Link](file)
[Link](["Product ID", "Product Name", "Quantity",
"Price"])
[Link]([101, "Laptop", 10, 50000])
[Link]([102, "Smartphone", 15, 20000])

with open("[Link]", "r") as file:


print([Link]())

Option 2: MySQL Database Operations

import [Link]

conn = [Link](host="localhost", user="root",


password="password", database="school")
cur = [Link]()
[Link]("CREATE TABLE IF NOT EXISTS students (student_id INT,
student_name VARCHAR(50), age INT)")
[Link]("INSERT INTO students VALUES (1, 'Alice', 16)")
[Link]("INSERT INTO students VALUES (2, 'Bob', 17)")
[Link]("INSERT INTO students VALUES (3, 'Charlie', 18)")
[Link]("SELECT * FROM students")
print([Link]())

[Link]()
[Link]()
Question 2 practical paper set 3

-- 1. Orders with amount > 1000

SELECT * FROM orders WHERE order_amount > 1000;

-- 2. Customer names and phone numbers

SELECT customer_name, phone_number FROM customers;

-- 3. Orders on '2025-01-02'

SELECT * FROM orders WHERE order_date = '2025-01-02';

-- 4. Update order_amount for order_id = 1001

UPDATE orders SET order_amount = 600 WHERE order_id = 1001;

-- 5. Delete customer with customer_id = 203

DELETE FROM customers WHERE customer_id = 203;

You might also like