0% found this document useful (0 votes)
18 views

SQL - I: - Input by User - Desired Output - Computer Generated Message

The document contains a series of questions and answers related to SQL queries on a student database table. The student table contains fields like roll number, admission number, name, class, section, date of birth, and total marks. Various SQL queries are performed to insert data, display the table structure, select specific fields, and use pattern matching to filter records.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

SQL - I: - Input by User - Desired Output - Computer Generated Message

The document contains a series of questions and answers related to SQL queries on a student database table. The student table contains fields like roll number, admission number, name, class, section, date of birth, and total marks. Various SQL queries are performed to insert data, display the table structure, select specific fields, and use pattern matching to filter records.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

SQL – I

█ - Input by User
█ - Desired Output
█ - Computer Generated Message

Enter password: *********


Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.30 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its


affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

Q.25:
Create a table ‘student’, in a newly created database ‘12_C’, with the following data structure:
student
rno admno name cl_sec dob tot_m

A.25:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
6 rows in set (0.02 sec)

mysql> create database 12_C;


Query OK, 1 row affected (0.01 sec)

mysql> use 12_C;


Database changed

mysql> show tables;


Empty set (0.01 sec)

mysql> create table student


-> (rno int(3),
-> admno varchar(5) primary key,
-> name varchar(30),
-> cl_sec varchar(5),
-> dob date,
-> tot_m int(3));
Query OK, 0 rows affected, 2 warnings (0.03 sec)

Q.26:
Add records in the table ‘student’.

A.26:
(i)
mysql> insert into student values(42, '29805', 'Revanta Choudhary', '12-C', '2005-08-19', '99');
Query OK, 1 row affected (0.01 sec)

(ii)
mysql> insert into student(admno, name) values('12501', 'Smrit Singh');
Query OK, 1 row affected (0.00 sec)

(iii)
mysql> insert into student values(NULL, '51287', 'Amogh Chary', '12-A', NULL, NULL);
Query OK, 1 row affected (0.00 sec)

Q.27:
Display the structure of the table ‘student’.

A.27:
mysql> desc student;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| rno | int | YES | | NULL | |
| admno | varchar(5) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| cl_sec | varchar(5) | YES | | NULL | |
| dob | date | YES | | NULL | |
| tot_m | int | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
6 rows in set (0.01 sec)

Q.28:
Display all the contents of the table ‘student’.
A.28:
mysql> select * from student;
+------+-------+-------------------+--------+------------+-------+
| rno | admno | name | cl_sec | dob | tot_m |
+------+-------+-------------------+--------+------------+-------+
| NULL | 12501 | Smrit Singh | NULL | NULL | NULL |
| 42 | 29805 | Revanta Choudhary | 12-C | 2005-08-19 | 99 |
| NULL | 51287 | Amogh Chary | 12-A | NULL | NULL |
+------+-------+-------------------+--------+------------+-------+
3 rows in set (0.00 sec)

SQL – II (Selective Retrieval)


Q.29:
Display the roll number, name, class and section of all the students.

A.29:
mysql> select rno, name, cl_sec from student;
+------+-------------------+--------+
| rno | name | cl_sec |
+------+-------------------+--------+
| NULL | Smrit Singh | NULL |
| 42 | Revanta Choudhary | 12-C |
| NULL | Amogh Chary | 12-A |
+------+-------------------+--------+
3 rows in set (0.00 sec)

Q.30:
Display the admission number, name, class and section of all students in class in 12 – C.

A.30:
mysql> select admno, name, cl_sec from student where cl_sec = '12-C';
+-------+-------------------+--------+
| admno | name | cl_sec |
+-------+-------------------+--------+
| 29805 | Revanta Choudhary | 12-C |
+-------+-------------------+--------+
1 row in set (0.00 sec)

# Adding more students’ data:


(i)
mysql> insert into student values(11, '44871', 'Disha Aswal', '12-E', '2005-03-11', 75);
Query OK, 1 row affected (0.00 sec)
(ii)
mysql> insert into student(admno, cl_sec, tot_m) values('98871', '12-C', 88);
Query OK, 1 row affected (0.00 sec)

(iii)
mysql> insert into student(admno, name, dob) values('23322', 'Annanya Kumar', '2005-12-28');
Query OK, 1 row affected (0.00 sec)

Q.31:
Display roll number, name and date of birth of all the students born before 15 th June 2005.

A.31:
mysql> select rno, name, dob from student where dob < '2005-06-15';
+------+-------------+------------+
| rno | name | dob |
+------+-------------+------------+
| 11 | Disha Aswal | 2005-03-11 |
+------+-------------+------------+
1 row in set (0.00 sec)

Q.32:
Display admission number, class and section of all students born between 15 th June 2005 and
31st December 2005.

A.32:
(i)
mysql> select admno, name, cl_sec from student where dob >= '2005-06-15' and dob <= '2005-12-31';
+-------+-------------------+--------+
| admno | name | cl_sec |
+-------+-------------------+--------+
| 23322 | Annanya Kumar | NULL |
| 29805 | Revanta Choudhary | 12-C |
+-------+-------------------+--------+
2 rows in set (0.00 sec)

(ii)
mysql> select admno, name, cl_sec from student where dob between '2004-01-01' and '2006-01-01';
+-------+-------------------+--------+
| admno | name | cl_sec |
+-------+-------------------+--------+
| 23322 | Annanya Kumar | NULL |
| 29805 | Revanta Choudhary | 12-C |
| 44871 | Disha Aswal | 12-E |
+-------+-------------------+--------+
3 rows in set (0.00 sec)
Q.33:
(i) Display admission number, name, class and section of all students of 12 - A, 12 – B and 12 –
C.

(ii) Display admission number, name, class and section of all students of 12 - A, 12 – D and 12 –
E.

A.33:
(i)
mysql> select admno, name, cl_sec from student where cl_sec = '12-A' or cl_sec = '12-B' or cl_sec = '12-
C';
+-------+-------------------+--------+
| admno | name | cl_sec |
+-------+-------------------+--------+
| 29805 | Revanta Choudhary | 12-C |
| 51287 | Amogh Chary | 12-A |
| 98871 | NULL | 12-C |
+-------+-------------------+--------+
3 rows in set (0.00 sec)

(ii)
mysql> select admno, name, cl_sec from student where cl_sec IN('12-A', '12-D', '12-E');
+-------+-------------+--------+
| admno | name | cl_sec |
+-------+-------------+--------+
| 44871 | Disha Aswal | 12-E |
| 51287 | Amogh Chary | 12-A |
+-------+-------------+--------+
2 rows in set (0.00 sec)

# Pattern Matching:

Q.34
(i) Display all details, of all students, whose full names begin with an ‘A’.

(ii) Display all details, of all students, whose full names end with a ‘y’.

A.34:
(i)
mysql> select * from student where name like 'A%';
+------+-------+---------------+--------+------------+-------+
| rno | admno | name | cl_sec | dob | tot_m |
+------+-------+---------------+--------+------------+-------+
| NULL | 23322 | Annanya Kumar | NULL | 2005-12-28 | NULL |
| NULL | 51287 | Amogh Chary | 12-A | NULL | NULL |
+------+-------+---------------+--------+------------+-------+
2 rows in set (0.00 sec)

(ii)
mysql> select * from student where name like '%y';
+------+-------+-------------------+--------+------------+-------+
| rno | admno | name | cl_sec | dob | tot_m |
+------+-------+-------------------+--------+------------+-------+
| 42 | 29805 | Revanta Choudhary | 12-C | 2005-08-19 | 99 |
| NULL | 51287 | Amogh Chary | 12-A | NULL | NULL |
+------+-------+-------------------+--------+------------+-------+
2 rows in set (0.00 sec)

Q.35:
(i) Display all details, of all students, whose full names have ‘an’ in them.

(ii) Display all details, of all students, whose class and sections have a ‘-’ in them.

A.35:
(i)
mysql> select * from student where name like '%an%';
+------+-------+-------------------+--------+------------+-------+
| rno | admno | name | cl_sec | dob | tot_m |
+------+-------+-------------------+--------+------------+-------+
| NULL | 23322 | Annanya Kumar | NULL | 2005-12-28 | NULL |
| 42 | 29805 | Revanta Choudhary | 12-C | 2005-08-19 | 99 |
+------+-------+-------------------+--------+------------+-------+
2 rows in set (0.00 sec)

(ii)
mysql> select * from student where cl_sec like '%-%';
+------+-------+-------------------+--------+------------+-------+
| rno | admno | name | cl_sec | dob | tot_m |
+------+-------+-------------------+--------+------------+-------+
| 42 | 29805 | Revanta Choudhary | 12-C | 2005-08-19 | 99 |
| 11 | 44871 | Disha Aswal | 12-E | 2005-03-11 | 75 |
| NULL | 51287 | Amogh Chary | 12-A | NULL | NULL |
| NULL | 98871 | NULL | 12-C | NULL | 88 |
+------+-------+-------------------+--------+------------+-------+
4 rows in set (0.00 sec)

Q.36:
(i) Display all details, of all students, whose full names’ second letter is an ‘e’.

(ii) Display all details, of all students, whose full name’ second last letter is an ‘a’.
A.36:
(i)
mysql> select * from student where name like '_e%';
+------+-------+-------------------+--------+------------+-------+
| rno | admno | name | cl_sec | dob | tot_m |
+------+-------+-------------------+--------+------------+-------+
| 42 | 29805 | Revanta Choudhary | 12-C | 2005-08-19 | 99 |
+------+-------+-------------------+--------+------------+-------+
1 row in set (0.00 sec)

(ii)
mysql> select * from student where name like '%a_';
+------+-------+---------------+--------+------------+-------+
| rno | admno | name | cl_sec | dob | tot_m |
+------+-------+---------------+--------+------------+-------+
| NULL | 23322 | Annanya Kumar | NULL | 2005-12-28 | NULL |
| 11 | 44871 | Disha Aswal | 12-E | 2005-03-11 | 75 |
+------+-------+---------------+--------+------------+-------+
2 rows in set (0.00 sec)

SQL - III
Q.37:
Display details of all class 12th students.

A.37:
mysql> select distinct(cl_sec) from student;
+--------+
| cl_sec |
+--------+
| NULL |
| 12-C |
| 12-E |
| 12-A |
+--------+
4 rows in set (0.00 sec)

Q.38:
Display the student details of those students whose date of birth hasn’t been entered.

A.38:
mysql> select * from student where dob IS NULL;
+------+-------+-------------+--------+------+-------+
| rno | admno | name | cl_sec | dob | tot_m |
+------+-------+-------------+--------+------+-------+
| NULL | 12501 | Smrit Singh | NULL | NULL | NULL |
| NULL | 51287 | Amogh Chary | 12-A | NULL | NULL |
| NULL | 98871 | NULL | 12-C | NULL | 88 |
+------+-------+-------------+--------+------+-------+
3 rows in set (0.00 sec)

Q.39:
(i) Change column aliases.

(ii) Change table alias.

A.39:
(i)
mysql> select rno as 'Roll Number', admno as 'Admission Number', name as 'Full Name', cl_sec as 'Class
and Section', dob as 'Date of Birth', tot_m as 'Total Marks' from student;
+-------------+------------------+-------------------+-------------------+---------------+-------------+
| Roll Number | Admission Number | Full Name | Class and Section | Date of Birth | Total Marks |
+-------------+------------------+-------------------+-------------------+---------------+-------------+
| NULL | 12501 | Smrit Singh | NULL | NULL | NULL |
| NULL | 23322 | Annanya Kumar | NULL | 2005-12-28 | NULL |
| 42 | 29805 | Revanta Choudhary | 12-C | 2005-08-19 | 99 |
| 11 | 44871 | Disha Aswal | 12-E | 2005-03-11 | 75 |
| NULL | 51287 | Amogh Chary | 12-A | NULL | NULL |
| NULL | 98871 | NULL | 12-C | NULL | 88 |
+-------------+------------------+-------------------+-------------------+---------------+-------------+
6 rows in set (0.00 sec)

(ii)
mysql> select * from student s;
+------+-------+-------------------+--------+------------+-------+
| rno | admno | name | cl_sec | dob | tot_m |
+------+-------+-------------------+--------+------------+-------+
| NULL | 12501 | Smrit Singh | NULL | NULL | NULL |
| NULL | 23322 | Annanya Kumar | NULL | 2005-12-28 | NULL |
| 42 | 29805 | Revanta Choudhary | 12-C | 2005-08-19 | 99 |
| 11 | 44871 | Disha Aswal | 12-E | 2005-03-11 | 75 |
| NULL | 51287 | Amogh Chary | 12-A | NULL | NULL |
| NULL | 98871 | NULL | 12-C | NULL | 88 |
+------+-------+-------------------+--------+------------+-------+
6 rows in set (0.00 sec)

# Having a look at the current status of the table:

mysql> select * from student;


+------+-------+-------------------+--------+------------+-------+
| rno | admno | name | cl_sec | dob | tot_m |
+------+-------+-------------------+--------+------------+-------+
| NULL | 12501 | Smrit Singh | NULL | NULL | NULL |
| NULL | 23322 | Annanya Kumar | NULL | 2005-12-28 | NULL |
| 42 | 29805 | Revanta Choudhary | 12-C | 2005-08-19 | 99 |
| 11 | 44871 | Disha Aswal | 12-E | 2005-03-11 | 75 |
| NULL | 51287 | Amogh Chary | 12-A | NULL | NULL |
| NULL | 98871 | NULL | 12-C | NULL | 88 |
+------+-------+-------------------+--------+------------+-------+
6 rows in set (0.00 sec)

# Scalar Expressions with Fields:

Q.40:
Increase the roll numbers of the students, whose roll numbers have already been provided, by
a factor of 10.

A.40:
mysql> select rno + 10, name from student;
+----------+-------------------+
| rno + 10 | name |
+----------+-------------------+
| NULL | Smrit Singh |
| NULL | Annanya Kumar |
| 52 | Revanta Choudhary |
| 21 | Disha Aswal |
| NULL | Amogh Chary |
| NULL | NULL |
+----------+-------------------+
6 rows in set (0.00 sec)

SQL - IV
# Aggregate Functions:

Q.41:
For the table ‘student’, give the highest and lowest date of births.

A.41:
mysql> select max(dob), min(dob) from student;
+------------+------------+
| max(dob) | min(dob) |
+------------+------------+
| 2005-12-28 | 2005-03-11 |
+------------+------------+
1 row in set (0.00 sec)

Q.42:
For the table ‘student’, give the names of the students with the highest and lowest date of
births.

A.42:
# Answer given by ma’am, in class, did not work.

Q.43:
Count and display the number of students whose class and section is 12 – C.

A.43:
mysql> select count(name) from student where cl_sec = '12-C';
+----------+
| count(name) |
+----------+
| 2|
+----------+
1 row in set (0.00 sec)

Q.44:
Find the average class marks.

A.44:
mysql> select avg(tot_m) from student;
+------------+
| avg(tot_m) |
+------------+
| 87.3333 |
+------------+
1 row in set (0.00 sec)

# ‘group by’ Command:

Q.45:
Count and display the number of students in each class.

A.45:
mysql> select cl_sec, count(cl_sec) from student group by cl_sec;
+--------+---------------+
| cl_sec | count(cl_sec) |
+--------+---------------+
| NULL | 0|
| 12-C | 2|
| 12-E | 1|
| 12-A | 1|
+--------+---------------+
4 rows in set (0.00 sec)

# Without the ‘group by’ command:

mysql> select cl_sec, count(cl_sec) from student;


+--------+---------------+
| cl_sec | count(cl_sec) |
+--------+---------------+
| NULL | 4|
+--------+---------------+
1 row in set (0.00 sec)

Q.46:
Calculate and display the class average marks, for each class.

A.46:
mysql> select cl_sec, avg(tot_m) as 'cl_avg' from student group by cl_sec;
+--------+---------+
| cl_sec | cl_avg |
+--------+---------+
| NULL | NULL |
| 12-C | 93.5000 |
| 12-E | 75.0000 |
| 12-A | NULL |
+--------+---------+
4 rows in set (0.00 sec)

Q.47:
Display the student details in descending order of their marks.

A.47:
mysql> select * from student order by tot_m desc;
+------+-------+-------------------+--------+------------+-------+
| rno | admno | name | cl_sec | dob | tot_m |
+------+-------+-------------------+--------+------------+-------+
| 42 | 29805 | Revanta Choudhary | 12-C | 2005-08-19 | 99 |
| NULL | 98871 | NULL | 12-C | NULL | 88 |
| 11 | 44871 | Disha Aswal | 12-E | 2005-03-11 | 75 |
| NULL | 12501 | Smrit Singh | NULL | NULL | NULL |
| NULL | 23322 | Annanya Kumar | NULL | 2005-12-28 | NULL |
| NULL | 51287 | Amogh Chary | 12-A | NULL | NULL |
+------+-------+-------------------+--------+------------+-------+
6 rows in set (0.00 sec)

# Without specifying whether we want it in descending or ascending order (Default is


ascending):
mysql> select * from student order by tot_m;
+------+-------+-------------------+--------+------------+-------+
| rno | admno | name | cl_sec | dob | tot_m |
+------+-------+-------------------+--------+------------+-------+
| NULL | 12501 | Smrit Singh | NULL | NULL | NULL |
| NULL | 23322 | Annanya Kumar | NULL | 2005-12-28 | NULL |
| NULL | 51287 | Amogh Chary | 12-A | NULL | NULL |
| 11 | 44871 | Disha Aswal | 12-E | 2005-03-11 | 75 |
| NULL | 98871 | NULL | 12-C | NULL | 88 |
| 42 | 29805 | Revanta Choudhary | 12-C | 2005-08-19 | 99 |
+------+-------+-------------------+--------+------------+-------+
6 rows in set (0.00 sec)

Q.48:
Display the average marks of each class in ascending order.

A.48:
mysql> select avg(tot_m) as 'cl_avg' from student order by cl_avg;
+---------+
| cl_avg |
+---------+
| 87.3333 |
+---------+
1 row in set (0.00 sec)

# UPDATE Command:
Q.49:
Increase the total marks of students whose marks are already provided by 1 mark.

A.49:
mysql> update student set tot_m = tot_m + 1;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 6 Changed: 3 Warnings: 0

Q.50:
Increase the marks of students of 12 – C, and whose marks are already provided, by 10%.

A.50:
mysql> update student set tot_m = tot_m + tot_m/ 10 where cl_sec = '12-C';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0

# Questions 51 to 56 are of Python file handling.

# Viewing databases and tables for clarity:


mysql> show databases;
+--------------------+
| Database |
+--------------------+
| 12_c |
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
7 rows in set (0.00 sec)

mysql> use 12_C;


Database changed

mysql> show tables;


+----------------+
| Tables_in_12_c |
+----------------+
| student |
+----------------+
1 row in set (0.00 sec)

mysql> desc student;


+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| admno | varchar(5) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| cl_sec | varchar(5) | YES | | NULL | |
| dob | date | YES | | NULL | |
| tot_m | int | YES | | NULL | |
| rno | int | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

Q.57:
Modify the table ‘student’ and update the field ‘rno’ to varchar(5).

A.57:
mysql> alter table student modify rno varchar(5);
Query OK, 6 rows affected (0.06 sec)
Records: 6 Duplicates: 0 Warnings: 0

# Checking:
mysql> desc student;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| admno | varchar(5) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| cl_sec | varchar(5) | YES | | NULL | |
| dob | date | YES | | NULL | |
| tot_m | int | YES | | NULL | |
| rno | varchar(5) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

Q.58:
Delete the field ‘rno’.

A.58:
mysql> alter table student drop rno;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

# Checking:

mysql> desc student;


+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| admno | varchar(5) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| cl_sec | varchar(5) | YES | | NULL | |
| dob | date | YES | | NULL | |
| tot_m | int | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

Q.59:
Delete all students from:
(a) a table ‘example’, whose classes and sections are ’12-D’ respectively.
(b) a table ‘example’.

A.59:
# Creating the table first:
mysql> create table example
-> (name varchar(25) primary key,
-> cl_sec varchar(5));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into example values('Ross Geller', '12-D');
Query OK, 1 row affected (0.01 sec)

mysql> insert into example values('Monica Geller', '12-E');


Query OK, 1 row affected (0.01 sec)

mysql> insert into example values('Rachel Green', '12-A');


Query OK, 1 row affected (0.01 sec)

mysql> insert into example values('Joey Tribbiani', '12-B');


Query OK, 1 row affected (0.00 sec)

mysql> insert into example values('Chandler Bing', '12-C');


Query OK, 1 row affected (0.01 sec)

mysql> insert into example values('Phoebe Buffay', '12-D');


Query OK, 1 row affected (0.01 sec)

# Checking:

mysql> desc example;


+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name | varchar(25) | NO | PRI | NULL | |
| cl_sec | varchar(5) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> select * from example;


+----------------+--------+
| name | cl_sec |
+----------------+--------+
| Chandler Bing | 12-C |
| Joey Tribbiani | 12-B |
| Monica Geller | 12-E |
| Phoebe Buffay | 12-D |
| Rachel Green | 12-A |
| Ross Geller | 12-D |
+----------------+--------+
6 rows in set (0.00 sec)

(a)
mysql> delete from example where cl_sec = '12-D';
Query OK, 2 rows affected (0.01 sec)
#Checking:

mysql> select * from example;


+----------------+--------+
| name | cl_sec |
+----------------+--------+
| Chandler Bing | 12-C |
| Joey Tribbiani | 12-B |
| Monica Geller | 12-E |
| Rachel Green | 12-A |
+----------------+--------+
4 rows in set (0.00 sec)

(b)
mysql> delete from example;
Query OK, 4 rows affected (0.01 sec)

# Checking:

mysql> select * from example;


Empty set (0.00 sec)

Q.60:
Delete a table structure.
A.60:
(a)
mysql> drop table example;
Query OK, 0 rows affected (0.02 sec)

(b)
mysql> drop table if exists example;
Query OK, 0 rows affected, 1 warning (0.01 sec)

Q.61:
For the table ‘sports’ with the following structure:
sports  Table Name
stu_no  Student number field [int(5)]
cl_sec  Class and section field [varchar(5)]
nm  Name field [varchar(25)]
gm1  Game 1 field [varchar(20)]
gr1  Grade 1 field [varchar(5)]
gm2  Game 2 field [varchar(20)]
gr2  Grade 2 field [varchar(5)]

(a) Display the names of the students who have the grade ‘C’ in either game 1 or game 2 or
both.
(b) Display the names and classes of the students who have the same games for game 1 and 2.
(c) Display the games taken up by the students whose name starts with an ‘A’.
(d) Display the classes and names of those students who have a grade ‘A’ or higher.
(e) Display the number of students who have taken ‘Cricket’ in game 1.
(f) Display the different games taken up by a student.

A.61:
# Creating the table first:
mysql> create table sports
-> (stu_no int(5) primary key,
-> cl_sec varchar(5),
-> nm varchar(25),
-> gm1 varchar(20),
-> gr1 varchar(5),
-> gm2 varchar(20),
-> gr2 varchar(5));
Query OK, 0 rows affected (0.03 sec)

mysql> insert into sports values(30, '12-B', 'Amogh Chary', 'Cricket', 'C', 'Badminton', 'C');
Query OK, 1 row affected (0.01 sec)

mysql> insert into sports values(1, '12-A', 'Annanya Kumar', 'Hockey', 'A+', 'Squash', 'A');
Query OK, 1 row affected (0.00 sec)

mysql> insert into sports values(8, '12-E', 'Disha Aswal', 'Tennis', 'E', 'Badminton', 'C');
Query OK, 1 row affected (0.00 sec)

mysql> insert into sports values(35, '12-C', 'Revanta Choudhary', 'Table Tennis', 'A++', 'Football', 'A');
Query OK, 1 row affected (0.00 sec)

mysql> insert into sports values(98, '12-F', 'Shreya Sharma', 'Baseball', 'A', 'Baseball', 'B');
Query OK, 1 row affected (0.00 sec)

mysql> insert into sports values(11, '12-D', 'Smrit Singh', 'Cricket', 'B', 'Swimming', 'D');
Query OK, 1 row affected (0.00 sec)

# Checking:

mysql> desc sports;


+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| stu_no | int | NO | PRI | NULL | |
| cl_sec | varchar(5) | YES | | NULL | |
| nm | varchar(25) | YES | | NULL | |
| gm1 | varchar(20) | YES | | NULL | |
| gr1 | varchar(5) | YES | | NULL | |
| gm2 | varchar(20) | YES | | NULL | |
| gr2 | varchar(5) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

mysql> select * from sports;


+--------+--------+-------------------+--------------+------+-----------+------+
| stu_no | cl_sec | nm | gm1 | gr1 | gm2 | gr2 |
+--------+--------+-------------------+--------------+------+-----------+------+
| 1 | 12-A | Annanya Kumar | Hockey | A+ | Squash | A |
| 8 | 12-E | Disha Aswal | Tennis | E | Badminton | C |
| 11 | 12-D | Smrit Singh | Cricket | B | Swimming | D |
| 30 | 12-B | Amogh Chary | Cricket | C | Badminton | C |
| 35 | 12-C | Revanta Choudhary | Table Tennis | A++ | Football | A |
| 98 | 12-F | Shreya Sharma | Baseball | A | Baseball | B |
+--------+--------+-------------------+--------------+------+-----------+------+
6 rows in set (0.00 sec)

(a)
mysql> select nm as 'FULL NAME' from sports where gr1 = 'C' or gr2 = 'C';
+-------------+
| FULL NAME |
+-------------+
| Disha Aswal |
| Amogh Chary |
+-------------+
2 rows in set (0.00 sec)

(b)
mysql> select nm as 'FULL NAME', cl_sec as 'CLASS AND SECTION' from sports where gm1 = gm2;
+---------------+-------------------+
| FULL NAME | CLASS AND SECTION |
+---------------+-------------------+
| Shreya Sharma | 12-F |
+---------------+-------------------+
1 row in set (0.00 sec)

(c)
mysql> select gm1 as 'GAME 1', gm2 as 'GAME 2' from sports where nm like 'A%';
+---------+-----------+
| GAME 1 | GAME 2 |
+---------+-----------+
| Hockey | Squash |
| Cricket | Badminton |
+---------+-----------+
2 rows in set (0.00 sec)
(d)
mysql> select cl_sec as 'CLASS AND SECTION', nm as 'FULL NAME' from sports where gr1 = 'A' or gr2 =
'A';
+-------------------+-------------------+
| CLASS AND SECTION | FULL NAME |
+-------------------+-------------------+
| 12-A | Annanya Kumar |
| 12-C | Revanta Choudhary |
| 12-F | Shreya Sharma |
+-------------------+-------------------+
3 rows in set (0.00 sec)

(e)
mysql> select count(nm) as 'NUMBER OF STUDENTS WHO HAVE TAKEN CRICKET AS THEIR FIRST GAME'
from sports where gm1 = 'Cricket';
+---------------------------------------------------------------+
| NUMBER OF STUDENTS WHO HAVE TAKEN CRICKET AS THEIR FIRST GAME |
+---------------------------------------------------------------+
| 2|
+---------------------------------------------------------------+
1 row in set (0.00 sec)

(f)
mysql> select gm1 as 'GAME 1', gm2 as 'GAME 2' from sports;
+--------------+-----------+
| GAME 1 | GAME 2 |
+--------------+-----------+
| Hockey | Squash |
| Tennis | Badminton |
| Cricket | Swimming |
| Cricket | Badminton |
| Table Tennis | Football |
| Baseball | Baseball |
+--------------+-----------+
6 rows in set (0.00 sec)

# Future Code:

mysql>

You might also like