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

Dbms Ope Friday-2

The document contains SQL queries for various tasks, such as retrieving match details, manager names, student information, product ratings, and book issues based on specific criteria. Additionally, it includes a Python program that connects to a PostgreSQL database to fetch a user's email based on their user ID from a file. The document also outlines the necessary database connection parameters and error handling in Python.

Uploaded by

coolsiva1812
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)
6 views6 pages

Dbms Ope Friday-2

The document contains SQL queries for various tasks, such as retrieving match details, manager names, student information, product ratings, and book issues based on specific criteria. Additionally, it includes a Python program that connects to a PostgreSQL database to fetch a user's email based on their user ID from a file. The document also outlines the necessary database connection parameters and error handling in Python.

Uploaded by

coolsiva1812
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/ 6

SQL Questions -

1.​ Write a SQL query to find the host team id and the guest team id of the
matches played before MAY 15, 2020, in which the host team scored
more than 2 goals. [FLIS]

select host_team_id, guest_team_id


from matches
where match_date < ‘2020-05-15’ and host_team_score > 2

2.​ Write a SQL query to find the name of the managers whose names
contain at least six characters. [FLIS]

select name
from managers
where name like ‘______%’

Or,

select name
from managers
where length(name) >= 6

3.​ Write a SQL query to find the member type and member number of the
female students who are from the department code ‘EE’. [LIS]

select member_type, member_no


from members m inner join students s on m.roll_no = s.roll_no
where gender = ‘F’ and department_code = ‘EE’
4.​ Write a SQL query to find the name of the products with their average
ratings which have an average rating greater than 3. [Eshop]

select product_name, avg(rating)


from product p inner join reviews r on p.product_id = r.product_id
group by product_name
having avg(rating) > 3

5.​ Write a SQL query to find the name of the product ordered by user
‘EVIE’ on ‘2023-11-05’. [Eshop]

select product_name
from product p
join orders o on p.product_id = o.product_id
join users u on o.user_id = u.user_id
where user_name = ‘EVIE’ and date_ordered = ‘2023-11-05’

Or,

select product_name
from product
where product_id = (
​ select product_id
​ from orders
​ where date_ordered = ‘2023-11-05’ and user_id = (
​ ​ select user_id
​ ​ from users
​ ​ where user_name = ‘EVIE’
)
)
6.​ Write a SQL query to find the name of referees who were the fourth
referee for match numbers ‘M0001’, ‘M0003’, ‘M0005’. [FLIS]

select name
from referees r
inner join match_referees mr on r.referee_id = mr.fourth_referee
where match_num in(‘M0001’, ‘M0003’, ‘M0005’)

7.​ Write a SQL query to find the title, date of issue (doi) of the books issued
by the students whose first name starts with ‘S’ and from departments
‘Mechanical Engineering’ or ‘Computer Science’. [LIS]

select title, doi


from book_catalogue bc
join book_copies bcs on bc.isbn_no = bcs.isbn_no
join book_issue bi on bcs.accession_no = bi.accession_no
join members m on bi.member_no = m.member_no
join students s on m.roll_no = s.roll_no
join departments d on s.department_code = d.department_code
where student_fname like ‘S%’
and department_name in(‘Mechanical Engineering’, ‘Computer
Science’)

Or,

select title, doi


from book_catalogue bc
join book_copies bcs on bc.isbn_no = bcs.isbn_no
join book_issue bi on bcs.accession_no = bi.accession_no
join members m on bi.member_no = m.member_no
join students s on m.roll_no = s.roll_no
where student_fname like ‘S%’
and department_code in(‘ME’, ‘CS’)
Python-PostgreSQL Questions -

8.​ For the database connection, use the following connection string
variables:

database = sys.argv[1]​ //name of the database is obtained from the


command line argument
user = os.environ.get(‘PGUSER’)
password = os.environ.get(‘PGPASSWORD’)
host = os.environ.get(‘PGHOST’)
port = os.environ.get(‘PGPORT’)

Problem Statement:

Users
user_id varchar(20)
name varchar(30)
dob date
email varchar(50)
phone_num int

Write a Python program to print the email id of the user.


The user id of the user is given in a file named ‘user.txt’ resides in the
same folder as the python program file.
The output of the python program is only email id.
For example, if the user_id of the student is ‘CLAUS’.
Then output must be [email protected] only.
Note: No spaces.
# Importing necessary libraries
import psycopg2, sys, os

# Opening the given file to read the data inside


file = open("user.txt", "r")
uid = file.read()

# Closing the file


file.close() # Not a necessary step

# Using try-except block to handle errors


try:
# Creating the connection
connection = psycopg2.connect(
database = sys.argv[1],
user = os.environ.get(“PGUSER”),
password = os.environ.get(“PGPASSWORD”),
host = os.environ.get(“PGHOST”),
port = os.environ.get(“PGPORT”))

# Creating the cursor


cursor = connection.cursor()

# executing the query


query = f"select email from users where user_id = '{uid}'"
cursor.execute(query)

# Obtaining the result


result = cursor.fetchall() # or fetchmany() or fetchone()
for r in result:
print(r[0]) # Extracting the output we require

# Closing the cursor


cursor.close()

# This except block will catch both the general Python exception and
PostgreSQL(Database) related errors and print them to the console
except(Exception, psycopg2.DatabaseError) as error:
print(error)

# Closing the connection


finally:
connection.close()
9.​ SAME QUESTION AS 8

referee_id → name

You might also like