0% found this document useful (0 votes)
25 views12 pages

CS FAQ 1-12

Uploaded by

antrikshkachhap4
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)
25 views12 pages

CS FAQ 1-12

Uploaded by

antrikshkachhap4
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/ 12

​Ans 1.

a)
CREATE TABLE Product (
PCode VARCHAR(5) PRIMARY KEY,
PName VARCHAR(50) NOT NULL,
UPrice DECIMAL(10, 2) NOT NULL,
Manufacturer VARCHAR(50) NOT NULL
);

b)
The primary key is PCode.

c)
SELECT PCode, PName, UPrice
FROM Product
ORDER BY PName DESC, UPrice ASC;

d)
ALTER TABLE Product
ADD Discount DECIMAL(10, 2) DEFAULT 0;

e)
UPDATE Product
SET Discount = CASE
WHEN UPrice > 100 THEN UPrice * 0.10
ELSE 0
END;

f)
UPDATE Product
SET UPrice = UPrice * 1.12
WHERE Manufacturer = 'Dove';

g)
SELECT Manufacturer, COUNT(*) AS TotalProducts
FROM Product
GROUP BY Manufacturer;

Ans 2.
a)
PName avg(UPrice)
------------------- ------------
Washing Powder 120
Toothpaste 59.5
Soap 31.5
Shampoo 245

b)
Manufacturer
-------------
Surf
Colgate
Lux
Pepsodent
Dove

c)
COUNT(DISTINCT PName)
-----------------------
4

d)
PName MAX(UPrice) MIN(UPrice)
------------------- ------------ ------------
Washing Powder 120 120
Toothpaste 65 54
Soap 38 25
Shampoo 245 245

e)
PCode PName UPrice Manufacturer
------ ---------------- -------- --------------
P03 Soap 25 Lux
P02 Toothpaste 54 Colgate
P05 Soap 38 Dove
P04 Toothpaste 65 Pepsodent
P01 Washing Powder 120 Surf
P06 Shampoo 245 Dove

Ans 3.
Answers :
a. Define RDBMS. Name any two RDBMS software.

RDBMS (Relational Database Management System) is a type of


database management system (DBMS) that stores data in a
structured format using rows and columns (tables). It allows for
creating, managing, and querying relational databases using SQL.

Two RDBMS software:

MySQL

Oracle Database

b. What is the purpose of the following clauses in a SELECT


statement?

i) ORDER BY

The ORDER BY clause is used to sort the result set in either


ascending (ASC) or descending (DESC) order based on one or more
columns.

ii) GROUP BY

The GROUP BY clause groups rows that have the same values into
summary rows, like "total" or "count," based on one or more
columns.

c. Differentiate between DDL and DML statements.


DDL (Data Definition Language): These statements are used to
define and modify database structures (like tables, schemas, and
indexes).

Examples: CREATE, ALTER, DROP

DML (Data Manipulation Language): These statements are used to


manipulate and retrieve data in database tables.

Examples: SELECT, INSERT, UPDATE, DELETE

d. Explain the Primary Key, Candidate Key, Foreign Key, and


Alternate Key.

Primary Key: A unique key that uniquely identifies each record in a


table. It cannot contain NULL values.

Candidate Key: A set of columns that can uniquely identify a record


in a table. A table can have multiple candidate keys, but only one
will be chosen as the primary key.

Foreign Key: A key used to link two tables together. It is a column in


one table that refers to the primary key of another table.

Alternate Key: A candidate key that is not chosen as the primary


key. It can still uniquely identify a record but is not used as the
primary key.

e. What do you understand by Cartesian Product?

Cartesian Product refers to the result of combining each row from


one table with every row from another table in a query. This
happens when two tables are joined without any condition. It can
lead to a large number of rows being returned.

Example: If Table A has 3 rows and Table B has 4 rows, the


Cartesian product will return 3 * 4 = 12 rows.

Ans 4.
a)
SELECT * FROM Movie;

b)
SELECT MovieID, MovieName, (ProductionCost + BusinessCost) AS
Total_Earning
FROM Movie;

c)
SELECT DISTINCT Category FROM Movie;

d)
SELECT MovieID, MovieName, (BusinessCost - ProductionCost) AS
NetProfit
FROM Movie;

e)
SELECT MovieID, MovieName, ProductionCost
FROM Movie
WHERE ProductionCost > 10000 AND ProductionCost < 100000;

f)
SELECT * FROM Movie
WHERE Category IN ('Comedy', 'Action');

g)
SELECT * FROM Movie
WHERE ReleaseDate IS NULL OR ReleaseDate = ' ';

Ans 5.
a)
SELECT MatchID
FROM MATCH_DETAILS
WHERE FirstTeamScore > 70 AND SecondTeamScore > 70;

b)
SELECT MatchID
FROM MATCH_DETAILS
WHERE FirstTeamScore < 70 AND SecondTeamScore > 70;

c)
SELECT MatchID, MatchDate
FROM MATCH_DETAILS
WHERE (FirstTeam = 1 AND FirstTeamScore > SecondTeamScore)
OR (SecondTeam = 1 AND SecondTeamScore > FirstTeamScore);

d)
SELECT MatchID
FROM MATCH_DETAILS
WHERE (FirstTeam = 2 AND FirstTeamScore <= SecondTeamScore)
OR (SecondTeam = 2 AND SecondTeamScore <= FirstTeamScore);

e)
ALTER TABLE TEAM RENAME TO T_DATA;

ALTER TABLE T_DATA RENAME COLUMN TeamID TO T_ID;


ALTER TABLE T_DATA RENAME COLUMN TeamName TO T_NAME;

Ans 6.
SQL Constraints:

NOT NULL: Ensures that a column cannot have a NULL value.

PRIMARY KEY: Uniquely identifies each record in a table. A


primary key must be unique and not NULL.

FOREIGN KEY: A key used to link two tables together. It ensures


that the value in one table matches a value in another table.

UNIQUE: Ensures that all values in a column are unique.

CHECK: Ensures that all values in a column satisfy a condition.

DEFAULT: Provides a default value for a column when no value is


specified.
INDEX: Improves query performance by creating a searchable
index.

Ans 7.
Read Operations:

fetchone(): Fetches the next row of a query result, returning a


single record as a tuple.

fetchmany(n): Fetches the next n rows of a query result, returning


a list of tuples.

fetchall(): Fetches all the rows of a query result, returning them as


a list of tuples.

Ans 8.
Python program to create a Table student inside school database:

import sqlite3
conn = sqlite3.connect('school.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS student (
student_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER,
grade TEXT
)
''')
conn.commit()
conn.close()

Ans 9.
Python program to insert multiple records into the student table:

import sqlite3
conn = sqlite3.connect('school.db')
cursor = conn.cursor()
students = [
(1, 'Alice', 20, 'A'),
(2, 'Bob', 22, 'B'),
(3, 'Charlie', 21, 'A'),
(4, 'David', 23, 'C')
]
cursor.executemany('''
INSERT INTO student (student_id, name, age, grade)
VALUES (?, ?, ?, ?)
''', students)
conn.commit()
conn.close()

Ans 10.
Python program to display all the records of student:

import sqlite3
conn = sqlite3.connect('school.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM student')
records = cursor.fetchall()
for record in records:
print(record)
conn.close()

Ans 11.
i)
UPDATE Personal
SET Salary = Salary * 1.05
WHERE Allowance IS NOT NULL;

ii)
SELECT Name, (Salary + Allowance) AS "Total Salary"
FROM Personal;

iii)
DELETE FROM Personal
WHERE Salary > 25000;

Ans 12.
i)
SELECT P.PName, B.BNAME
FROM PRODUCT P
JOIN BRAND B ON P.BID = B.BID;
ii)
DESCRIBE PRODUCT;

iii)
SELECT B.BNAME, AVG(P.Rating) AS Avg_Rating
FROM PRODUCT P
JOIN BRAND B ON P.BID = B.BID
WHERE B.BNAME IN ('HP',
'DELL')
GROUP BY B.BNAME;

iv)
SELECT P.PName, P.UPrice, P.Rating
FROM PRODUCT P
ORDER BY P.Rating DESC;

Thank you for reading. ^⁠_^


You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy