Advertisements
Advertisements
Differentiate between count() and count(*) functions in SQL with appropriate examples.
Concept: Functions in SQL
Consider the following tables – Bank_Account and Branch:
Table: Bank_Account
| ACode | Name | Type |
| A01 | Amrita | Savings |
| A02 | Parthodas | Current |
| A03 | Miraben | Current |
Table: Branch
| ACode | City |
| A01 | Delhi |
| A02 | Mumbai |
| A01 | Nagpur |
What will be the output of the following statement?
SELECT * FROM Bank_Account NATURAL JOIN Branch;
Concept: SQL for Data Query
Write the output of the queries based on the table, TECH_COURSE given below:
| Table: TECH_COURSE | ||||
| CID | CNAME | FEES | STARTDATE | TID |
| C201 | Animation and VFX | 12000 | 2022-07-02 | 101 |
| C202 | CADD | 15000 | 2021-11-15 | NULL |
| C203 | DCA | 10000 | 2020-10-01 | 102 |
| C204 | DDTP | 9000 | 2021-09-15 | 104 |
| C205 | Mobile Application Development |
18000 | 2022-11-01 | 101 |
| C206 | Digital Marketing | 16000 | 2022-07-25 | 103 |
- SELECT DISTINCT TID FROM TECH_COURSE;
- SELECT TID, COUNT(*), MIN(FEES) FROM TECH_COURSE GROUP BY TID HAVING COUNT(TID)>1;
- SELECT CNAME FROM TECH_COURSE WHERE FEES>15000 ORDER BY CNAME;
- SELECT AVG(FEES) FROM TECH_COURSE WHERE FEES BETWEEN 15000 AND 17000;
Concept: GROUP BY Clause in SQL
Write the outputs of the SQL queries based on the relations Teacher and Placement given below:
Table: Teacher
| T_ID | Name | Age | Department | Date_of_join | Salary | Gender |
| 1 | Arunan | 34 | Computer Sc | 2019-01-10 | 12000 | M |
| 2 | Saman | 31 | History | 2017-03-24 | 20000 | F |
| 3 | Randeep | 32 | Mathematics | 2020-12-12 | 30000 | M |
| 4 | Samira | 35 | History | 2018-07-01 | 40000 | F |
| 5 | Raman | 42 | Mathematics | 2021-09-05 | 25000 | M |
| 6 | Shyam | 50 | History | 2019-06-27 | 30000 | M |
| 7 | Shiv | 44 | Computer Sc | 2019-02-25 | 21000 | M |
| 8 | Shalakha | 33 | Mathematics | 2018-07-31 | 20000 | F |
Table: Placement
| P_ID | Department | Place |
| 1 | History | Ahmedabad |
| 2 | Mathematics | Jaipur |
| 3 | Computer Sc | Nagpur |
- SELECT Department, avg(salary) FROM Teacher GROUP BY Department;
- SELECT MAX(Date_of_Join), MIN(Date_of_Join) FROM Teacher;
- SELECT Name, Salary, T.Department, Place FROM Teacher T, Placement P WHERE T.Department = P.Department AND Salary>20000;
- SELECT Name, Place FROM Teacher T, Placement P WHERE Gender = ’F’ AND T.Department = P.Department;
Concept: Functions in SQL
Write the command to view all tables in a database.
Concept: SQL for Data Definition
Predict the output of the code given below:
s="welcome2cs"
n = len(s)
m=""
for i in range(0, n):
if (s[i] >= 'a' and s[i] <= 'm'):
m = m +s[i].upper()
elif (s[i] >= 'n' and s[i] <= 'z'):
m = m +s[i-1]
elif (s[i].isupper()):
m = m + s[i].lower()
else:
m = m +'&'
print(m)Concept: Functions in SQL
The code given below reads the following record from the table named student and displays only those records that have marks greater than 75:
- RollNo - integer
- Name - string
- Clas - integer
- Marks - integer
Note the following to establish connectivity between Python and MYSQL:
- Username is root.
- The password is the tiger.
- The table exists in an MYSQL database named school.
Write the following missing statements to complete the code:
Statement 1 - to form the cursor object
Statement 2 - to execute the query that extracts records of those students whose marks are greater than 75.
Statement 3 - to read the complete result of the query (records whose marks are greater than 75) into the object named data, from the table student in the database.
import mysql.connector as mysql
def sql_data():
con1=mysql.connect(host="localhost",user="root",password="tiger", database="school")
mycursor=_______________ #Statement 1
print("Students with marks greater than 75 are :")
_________________________ #Statement 2
data=__________________ #Statement 3
for i in data:
print(i)
print()Concept: SQL for Data Definition
Navdeep creates a table RESULT with a set of records to maintain the marks secured by students in Sem1, Sem2, Sem3, and their divisions. After the creation of the table, he entered data of 7 students in the table.
| ROLL_NO | SNAME | SEM1 | SEM2 | SEM3 | DIVISION |
| 101 | KARAN | 366 | 410 | 402 | I |
| 102 | NAMAN | 300 | 350 | 325 | I |
| 103 | ISHA | 400 | 410 | 415 | I |
| 104 | RENU | 350 | 357 | 415 | I |
| 105 | ARPIT | 100 | 75 | 178 | IV |
| 106 | SABINA | 100 | 205 | 217 | II |
| 107 | NEELAM | 470 | 450 | 471 | I |
Based on the data given above answer the following questions:
- Identify the most appropriate column, which can be considered as the Primary key.
- If two columns are added and 2 rows are deleted from the table result, what will be the new degree and cardinality of the above table?
- Write the statements to:
a. Insert the following record into the table
Roll No - 108, Name - Aadit, Sem1- 470, Sem2 - 444, Sem3 - 475, Div - I.
b. Increase the SEM2 marks of the students 3% whose name begins with ‘N’.
OR
Write the statements to:
a. Delete the record of students securing IV division.
b. Add a column REMARKS in the table with datatype as varchar with 50 characters.
Concept: SQL for Data Definition
______ clause is used with a SELECT statement to display data in a sorted form with respect to a specified column.
Concept: SQL for Data Query
Which function returns the sum of all elements of a list?
Concept: Functions in SQL
Differentiate between CHAR and VARCHAR data types in SQL with appropriate examples.
Concept: Data Types and Constraints in MySQL
Name any two DML commands.
Concept: Data Updation and Deletion
Write the output of the queries (i) to (iv) based on the table, WORKER given below:
TABLE: WORKER
| W_ID | F_NAME | L_NAME | CITY | STATE |
| 102 | SAHIL | KHAN | KANPUR | UTTAR PRADESH |
| 104 | SAMEER | PARIKH | ROOP NAGAR | PUNJAB |
| 105 | MARY | JONES | DELHI | DELHI |
| 106 | MAHIR | SHARMA | SONIPAT | HARYANA |
| 107 | ATHARVA | BHARDWAJ | DELHI | DELHI |
| 108 | VEDA | SHARMA | KANPUR | UTTAR PRADESH |
SELECT F_NAME, CITY FROM WORKER ORDER BY STATE DESC;SELECT DISTINCT (CITY) FROM WORKER;SELECT F_NAME, STATE FROM WORKER WHERE L_NAME LIKE '_HA%';SELECT CITY, COUNT(*) FROM WORKER GROUP BY CITY;
Concept: SQL for Data Query
Write the command to view all databases.
Concept: SQL for Data Definition
Write the outputs of the SQL queries (i) to (iv) based on the relations COMPUTER and SALES given below:
| Table: COMPUTER | ||||
| PROD_ID | PROD_NAME | PRICE | COMPANY | TYPE |
| P001 | MOUSE | 200 | LOGITECH | INPUT |
| P002 | LASER PRINTER | 4000 | CANON | OUTPUT |
| P003 | KEYBOARD | 500 | LOGITECH | INPUT |
| P004 | JOYSTICK | 1000 | IBALL | INPUT |
| P005 | SPEAKER | 1200 | CREATIVE | OUTPUT |
| P006 | DESKET PRINTER | 4300 | CANON | OUTPUT |
| Table: SALES | ||
| PROD_ID | QTY_SOLD | QUARTER |
| P002 | 4 | 1 |
| P003 | 2 | 2 |
| P001 | 3 | 2 |
| P004 | 2 | 1 |
SELECT MIN(PRICE), MAX(PRICE) FROM COMPUTER;SELECT COMPANY, COUNT(*) FROM COMPUTER GROUP BY COMPANY HAVING COUNT(COMPANY) > 1;SELECT PROD_NAME, QTY_SOLD FROM COMPUTER C, SALES S WHERE C.PROD_ID=S.PROD_ID AND TYPE = 'INPUT';SELECT PROD_NAME, COMPANY, QUARTER FROM COMPUTER C, SALES S WHERE C.PROD_ID=S.PROD_ID;
Concept: SQL for Data Query
Name any two DDL commands.
Concept: SQL for Data Definition
The code given below deletes the record from the table employee, which contains the following record structure:
E_code - StringE_name - StringSal - IntegerCity - String
Note the following to establish connectivity between Python and MySQL:
- Username is
root - Password is
root - The table exists in a MySQL database named
emp. - The details
(E_code, E_name, Sal, City)are the attributes of the table.
Write the following statements to complete the code:
Statement 1 – to import the desired library.
Statement 2 – to execute the command that deletes the record with E_code as 'E101'.
Statement 3 – to delete the record permanently from the database. ____________
import ____________ as mysql #Statement1
def delete():
mydb=mysql.connect(host="localhost",use r="root",
passwd="root",database="emp")
mycursor=mydb.cursor()
____________ #Statement 2
____________ #Statement 3
print ("Record deleted") Concept: SQL for Data Definition
The code given below reads the following records from the table employee and displays only those records that have employees coming from the city 'Delhi':
E_code - StringE_name - StringSal - IntegerCity - String
Note the following to establish connectivity between Python and MySQL:
- Username is
root - Password is
root - The table exists in a MySQL database named
emp. - The details
(E_code,E_name,Sal,City)are the attributes of the table.
Write the following statements to complete the code:
Statement 1 - to import the desired library.
Statement 2 - to execute the query that fetches records of the employees/coming from the city ‘Delhi’.
Statement 3 - to read the complete data of the query (rows whose city is Delhi) into the object named details, from the table employee in the database.
import ____________ as mysql #Statement 1
def display():
mydb=mysql.connect(host="localhost",user="root"
passwd="root", database=" emp")
mycursor=mydb.cursor()
____________ #Statement 2
details = ____________ # Statement 3
for i in details:
print (i)Concept: SQL for Data Query
In a table in the MYSQL database, an attribute A of datatype varchar(20) has the value “Keshav”. The attribute B of datatype char(20) has the value “Meenakshi”. How many characters are occupied by attribute A and attribute B?
Concept: Data Types and Constraints in MySQL
Ms Shalini has just created a table named “Employee” containing columns Ename, Department and Salary. After creating the table, she realized that she has forgotten to add a primary key column in the table. Help her in writing an SQL command to add a primary key column EmpId of integer type to the table Employee.
Thereafter, write the command to insert the following record in the table:
EmpId- 999
Ename- Shweta
Department: Production
Salary: 26900Concept: SQL for Data Definition
