Part C: Python – SQL Connectivity (4 Programs with Outputs)
Question:1
Write a Python program to connect to a SQLite database named and
display a message confirming the connection.
Connect to Database
import sqlite3
conn = [Link]("[Link]")
print("Connected to database")
[Link]()
Output:
Connected to database
Question:2
Write a Python program to create a table named Employee with
columns id (integer) and name (text) in the [Link] database.
Ensure the table is created only if it does not already exist.
Create Table
import sqlite3
conn = [Link]("[Link]")
cur = [Link]()
[Link]("CREATE TABLE IF NOT EXISTS Employee(id INT,
name TEXT)")
[Link]()
[Link]()
Output:
Table created successfully
Question:3
Write a Python program to insert a record into the table in the
database.
Insert Data
import sqlite3
conn = [Link]("[Link]")
cur = [Link]()
[Link]("INSERT INTO Employee VALUES(1, 'John')")
[Link]()
[Link]()
Output:
1 row inserted
Question:4
Write a Python program to fetch all records from the Employee
table and display them.
Fetch Data
import sqlite3
conn = [Link]("[Link]")
cur = [Link]()
[Link]("SELECT * FROM Employee")
rows = [Link]()
for r in rows:
print(r)
[Link]()
Output:
(1, 'John')
Questions Based on SQL Programs
Program 1: Employee Records (Single Table) Question:
Write an SQL query to create an Employee table with columns id, name, and salary.
Insert at least two records and display all employees using a SELECT statement.
Output:
Program 2: Students Marks (Single Table) Question:
Create a Students table with columns roll, name, and marks. Insert at least three
records. Write a query to display the names and marks of students who scored more
than 80.
Program 3: Orders & Customers (Multi-table Join) Question:
Create two tables: Customers(customer_id, name) and Orders(order_id,
customer_id). Insert sample records. Write a query to display the order IDs along
with the customer names using an INNER JOIN.
Output:
Program 4: Employee Salaries (Aggregate Functions) Question:
Using the Employee table, write a query to find the average salary, maximum
salary, and minimum salary of employees.
Output:
Program 5: Employees by Department (Group By) Question:
Create a table EmployeeDept(id, name, department) and insert at least five
records across multiple departments. Write a query to count the number of employees
in each department using GROUP BY.