Important Questions on MySQL

Important Questions on MySQL

Ques1.

Assume that you are working in the IT Department of a Creative Art Gallery (CAG), which sells different forms of art creations like Paintings, Sculptures, etc.
The data of Art Creations and Artists are kept in two tables — Articles and Artists respectively.
Following are few records from these two tables:

Table: Articles

CODEA_CodeArticleDOCPrice
PL001A0001Painting2018-10-1920000
SC028A0004Sculpture2021-01-1516000
QL005A0003Quilling2024-04-243000

Table: Artists

A_CodeNamePhoneEmailDOB
A0001Roy595923r@CrAG.com1986-10-12
A0002Ghosh1122334ghosh@CrAG.com1972-02-05
A0003Gargi121212Gargi@CrAG.com1996-03-22
A0004Mustafa33333333Mf@CrAG.com2000-01-01

Note:

  • The tables contain many more records than shown here.
  • DOC is the Date of Creation of an Article.

As an employee of CAG, you are required to write the SQL queries for the following:

(i) To display all the records from the Articles table in descending order of Price.
(ii) To display the details of Articles which were created in the year 2020.
(iii) To display the structure of the Artists table.
(iv) To display the names of all artists whose article is ‘Painting’ through Equi Join.
(v) To display the names of all artists whose article is ‘Painting’ through Natural Join.

Show Answers:

(i) SELECT * FROM Articles
ORDER BY Price DESC;

(ii) SELECT * FROM Articles
WHERE YEAR(DOC) = 2020;

(iii) DESCRIBE Artists;

(iv) SELECT Name FROM Artists, Articles
WHERE Artists.A_Code = Articles.A_Code
AND Articles.Article = ‘Painting’;

(v) SELECT Name FROM Artists
NATURAL JOIN Articles
WHERE Article = ‘Painting’;

Ques2:

A table named THEATER, in CINEMA database, has the following structure:

FieldType
Th_IDchar(5)
Namevarchar(15)
Cityvarchar(15)
Locationvarchar(15)
Seatsint

Write a function Delete_Theatre() to input the value of Th_ID from the user and permanently delete the corresponding record from the table.

Assume the following for Python-Database connectivity:
Host: localhost
User: root
Password: Ex2025

Ques:3

Consider the table ORDERS given below and write the output of the SQL queries that follow:

ORDNOITEMQTYRATEORDATE
1001RICE231202023-09-10
1002PULSES131202023-10-18
1003RICE251102023-11-17
1004WHEAT28652023-12-25
1005PULSES161102024-01-15
1006WHEAT27552024-04-15
1007WHEAT25602024-04-30

(i) SELECT ITEM, SUM(QTY) FROM ORDERS GROUP BY ITEM;
(ii) SELECT ITEM, QTY FROM ORDERS WHERE ORDATE BETWEEN ‘2023-11-01’ AND ‘2023-12-31’;
(iii) SELECT ORDNO, ORDATE FROM ORDERS WHERE ITEM = ‘WHEAT’ AND RATE >= 60;

Ques 4: Consider the table Projects given below:

Table: Projects

P_idPnameLanguageStartdateEnddate
P001School Management SystemPython2023-01-122023-04-03
P002Hotel Management SystemC++2022-12-012023-02-02
P003Blood BankPython2023-02-112023-03-02
P004Payroll Management SystemPython2023-03-122023-06-02

Based on the given table, write SQL queries for the following:

(i) Add the constraint, primary key to column P_id in the existing table Projects.

(ii) To change the language to Python of the project whose id is P002.

(iii) To delete the table Projects from MySQL database along with its data.

Show Answers:

(i) ALTER TABLE Projects
ADD CONSTRAINT PK_Projects PRIMARY KEY (P_id);
OR(without named constraints)
ALTER TABLE Projects
ADD CONSTRAINT PRIMARY KEY (P_id);

(ii) UPDATE Projects SET Language = ‘Python’
WHERE P_id = ‘P002’ ;

(iii) DROP TABLE Projects;

Ques 5:

Consider the tables Admin and Transport given below:

Table: Admin

S_idS_nameAddressS_type
S001SandhyaRohiniDay Boarder
S002VedanshiRohtakDay Scholar
S003VibhuRaj NagarNULL
S004AtharvaRampurDay Boarder

Table: Transport

S_idBus_noStop_name
S002TSS10Sarai Kale Khan
S004TSS12Sainik Vihar
S005TSS10Kamla Nagar

Write SQL queries for the following:

(i) Display the student name and their stop name from the tables Admin and Transport.

Show Answer:

SELECT S_name, Stop_name FROM Admin, Transport
WHERE Admin.S_id = Transport.S_id;
OR
SELECT S_name, Stop_name FROM Admin
NATURAL JOIN Transport;

(ii) Display the number of students whose S_type is not known.

Answer:

SELECT COUNT(*) FROM Admin
WHERE S_type IS NULL;

(iii) Display all details of the students whose name starts with ‘V’.

Answer

SELECT * FROM Admin
WHERE S_name LIKE ‘V%’;

(iv) Display student id and address in alphabetical order of student name, from the table Admin.

Answer:

SELECT S_id, Address FROM Admin
ORDER BY S_name;

Ques 6:

Consider the table ORDERS given below and write the output of the SQL queries that follow:

Table: ORDERS

ORDNOITEMQTYRATEORDATE
1001RICE231202023-09-10
1002PULSES131202023-10-18
1003RICE251102023-11-17
1004WHEAT28652023-12-25
1005PULSES161102024-01-15
1006WHEAT27552024-04-15
1007WHEAT25602024-04-30

(i)

SELECT ITEM, SUM(QTY) 
FROM ORDERS 
GROUP BY ITEM;
Output:
ITEMSUM(QTY)
RICE48
PULSES29
WHEAT80

(ii)

SELECT ITEM, QTY 
FROM ORDERS 
WHERE ORDATE BETWEEN '2023-11-01' AND '2023-12-31';

Output:

Output:
ITEMQTY
RICE25
WHEAT28

(iii)

SELECT ORDNO, ORDATE 
FROM ORDERS 
WHERE ITEM = 'WHEAT' AND RATE >= 60;

Output:

Output:
ORDNOORDATE
10042023-12-25
10072024-04-30

MCQ on DBMS

Important MCQs Quizz on DBMS

1 / 30

Which of the following is an example of a DBMS?

2 / 30

commit() is required for ______ operations.

3 / 30

IN operator is used to ______.

4 / 30

SELECT COUNT(*) returns ______.

5 / 30

HAVING clause is used with ______.

6 / 30

WHERE clause is applied ______ GROUP BY.

7 / 30

Natural join removes ______.

8 / 30

A join that produces all combinations of two tables is ______.

9 / 30

To add a new column ‘age’, use:

10 / 30

To modify the structure of a table, use ______.

11 / 30

The wildcard character for any number of characters in LIKE is ______.

12 / 30

SELECT DISTINCT is used to  remove ______.

13 / 30

SUM() is a/an ______ function.

14 / 30

To sort data in ascending or descending, we use ______ clause.

15 / 30

NULL means ______.

16 / 30

To remove an entire table, we use ______.

17 / 30

NOT NULL is a ______.

18 / 30

Which of the following is a DML command?

19 / 30

A table can have ______ primary key(s).

20 / 30

Alternate key is ______.

21 / 30

A foreign key refers to ______.

22 / 30

A candidate key is ______.

23 / 30

A primary key must be ______.

24 / 30

Domain refers to ______.

25 / 30

The number of rows in a relation is called ______.

26 / 30

The number of columns in a relation is called ______.

27 / 30

Columns in a table are called ______.

28 / 30

Rows in a table are called ______.

29 / 30

Data redundancy means ______.

30 / 30

A table in a database is called a ______.

Your score is

The average score is 68%

0%

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top