0% found this document useful (0 votes)
76 views21 pages

MySQL Practical Exercises Guide

SQL practical

Uploaded by

preeti
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)
76 views21 pages

MySQL Practical Exercises Guide

SQL practical

Uploaded by

preeti
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

MySQL

PRACTICALS
PRACTICAL 20
Problem statement : Create a student table with the student id, name and
marks as attributes where the student id is the Primary key.

Solution :
Source code :
create table student
-> (
-> studentid int primary key ,
-> name varchar (40),
-> marks int (10)
-> );
Screenshot :
PRACTICAL 21
Problem statement : In the table ‘student’ created in practical 20, insert
the details of new students.

Solution :
Source code :
mysql> insert into student values (1,'Reena',100);
mysql> insert into student values (2,'Gauri',101);
mysql> insert into student values (3,'Sonu',99);
mysql> insert into student values (4,'Priyanshu',78);
mysql> insert into student values (5,'Sanjeev',48);

Screenshot :
PRACTICAL 22
Problem statement : Write SQL command to get the details of the
students with marks more than 90.

Solution :
Source code :

mysql> select * from student where marks > 90 ;

Screenshot :
PRACTICAL 23
Problem statement : Write SQL command to Find the min, max, sum, and
average of the marks in a student marks table.

Solution :
Source code :
mysql> select min(marks) as MINIMUM_MARKS , max(marks) as
MAXIMUM_MARKS,sum(marks) as TOTAL_MARKS ,
-> avg (marks) as AVERAGE_MARKS from student ;

Screenshot :
PRACTICAL 24
Problem statement : Delete the details of a student table created in
Practical 23.

Solution :
Source code :

mysql> delete from student;

Screenshot :
PRACTICAL 25
Problem statement :. : Find the total number of customers from each
country in the table (customer ID, customer Name, country) using group
by.

Solution :
Source code :

mysql> select country , count(customer_name) as 'total_customers' from


customer
-> group by country;

Screenshot :
PRACTICAL 26
Problem statement :. : Write a SQL query to order the (student ID, marks)
table in descending order of the marks.

Solution :
Source code :

mysql> select* from student1 order by name DESC;

Screenshot :
PRACTICAL 27
Problem statement : for the given table ‘Hospital’ write SQL command to
display name all patient admitted in month of June.
PID PNAME ADMITDATE DEPERT FEES
01 REENA RAJ 26/06/2020 Dentist 1000
02 GAURI RAJ 23/05/2020 Cardio 2000
03 SONU RAJAT 15/05/2020 ENT 3000
04 SHEELA JAIN 06/09/2020 Neuro 4000

Solution :
Source code :

mysql> select * from hospital where monthname(admitdate) = 'June' ;

Screenshot :
PRACTICAL 28
Problem statement : for the given table ‘Hospital’ write SQL command to
Display patient name in upper case with year of admission.
PID PNAME ADMITDATE DEPERT FEES
01 REENA RAJ 26/06/2020 Dentist 1000
02 GAURI RAJ 23/05/2020 Cardio 2000
03 SONU RAJAT 15/05/2020 ENT 3000
04 SHEELA JAIN 06/09/2020 Neuro 4000

Solution :
Source code :
mysql> Select UPPER(pname) as 'patient_name', Year(admitdate) as
'admit_year' from hospital;

Screenshot :
PRACTICAL 29
Problem statement : for the given table ‘Hospital’ Create sql query to
display first four letters of the patient name along with length of their
name who admitted before June.
PID PNAME ADMITDATE DEPERT FEES
01 REENA RAJ 26/06/2020 Dentist 1000
02 GAURI RAJ 23/05/2020 Cardio 2000
03 SONU RAJAT 15/05/2020 ENT 3000
04 SHEELA JAIN 06/09/2020 Neuro 4000

Solution :
Source code :
mysql> Select LEFT (pname ,4) as 'pname', length (pname) as 'length'
-> from hospital
-> where month (admitdate) < 6;

Screenshot :
PRACTICAL 30
Problem statement : Create a table name student2 containing attributes
Roll no ,Name ,Gender ,Marks ,DOB ,Mobile no and Stream.
Solution :
Source code :
mysql> create table student2(
-> rollno int ,
-> name varchar (10),
-> gender char (4),
-> marks int ,
-> dob date ,
-> mobile int ,
-> stream char (15)
-> );
Screenshot :
PRACTICAL 31
Problem statement : Insert values in table created in practical 30 .
Solution :
Source code :

mysql> insert into student2 values (1,'Reena','f',100,'2006-04-


23',1111,'commerce');
mysql> insert into student2 values (2,'Teena','f',80,'2006-06-
23',2222,'commerce');
mysql> insert into student2 values (3,'Shivam','m',75,'2009-06-
06',3333,'science');
mysql> insert into student2 values (4,'Sonu','m',85,'2008-12-
22',4444,'vocational');
mysql> insert into student2 values (5,'Priya','f',95,'2008-02-
29',5555,'science');
mysql> insert into student2 values (6,'Payal','f',94,'2007-05-
25',6666,'humanities');
mysql> insert into student2 values (7,'Monu','m',39,'2008-12-
25',7777,'vocational');
mysql> insert into student2 values (8,'Sandeep','m',99,'2006-01-
23',8888,'commerce');
mysql> insert into student2 values (9,'Purvi','f',74,'2002-05-
13',9999,'humanities');
mysql> insert into student2 values (10,'Sanjana','f',44,'2002-06-
17',9998,'science');
Screenshot :
PRACTICAL 32
SUM()
Problem statement : Displays the sum of all the marks in table student2
Solution :
Source code : mysql> select sum(marks) from student2;
Screenshot :

Problem statement : Displays the sum of all the marks greater than 70 in
the table
Solution :
Source code : mysql> select sum(marks) from student2 where marks> 70;
Screenshot
PRACTICAL 33
AVG()
Problem statement : : Displays the average of all the marks in the table
student2.
Solution :
Source code : mysql> select avg(marks) from student2 ;
Screenshot :

Problem statement : : Displays the average of all the marks in the table
student2 who born before 1st April,2005..
Solution :
Source code :
mysql> select avg(marks) from student2 where dob < '2005-04-01';
Screenshot :
PRACTICAL 34
MAX()
Problem statement : . To display maximum marks from student2 table for
female only.
Solution :
Source code :
mysql> select max(marks) from student2 where gender = 'f';

Screenshot :
PRACTICAL 35
Problem statement : Write SQL command to get the details of the
students with marks more than 90.

Solution :
Source code :

mysql> select * from student where marks > 90 ;

Screenshot :
PRACTICAL 22
Problem statement : Write SQL command to get the details of the
students with marks more than 90.

Solution :
Source code :

mysql> select * from student where marks > 90 ;

Screenshot :

Common questions

Powered by AI

The query is: SELECT LEFT(pname, 4) AS 'pname', LENGTH(pname) AS 'length' FROM hospital WHERE MONTH(admitdate) < 6. It extracts the first four letters and computes the length of patient names for admissions occurring before June .

The query to list students with marks over 90 is: SELECT * FROM student WHERE marks > 90. This retrieves all columns of students whose marks exceed 90 .

To determine the maximum marks scored by female students, use the SQL command: SELECT MAX(marks) FROM student2 WHERE gender = 'f'; This query filters the student2 table records by gender ('f' for female) and returns the highest marks .

The SQL query to order student IDs and marks by descending marks is: SELECT * FROM student1 ORDER BY marks DESC. This sorts the results based on the marks in descending order .

The SQL query to count the number of customers from each country would be: SELECT country, COUNT(customer_name) AS 'total_customers' FROM customer GROUP BY country. This groups the customers by their country and counts entries in each group .

Use the command DELETE FROM student; to remove all records from the student table. Considerations for using this command include ensuring that the data is no longer needed and understanding the permanent nature of this action, as it cannot be undone without a backup .

You can query the hospital database with the following SQL command: SELECT * FROM hospital WHERE MONTHNAME(admitdate) = 'June'. This command filters records to include only those with an admission date in June .

To find the minimum, maximum, sum, and average marks, you use: SELECT MIN(marks) AS MINIMUM_MARKS, MAX(marks) AS MAXIMUM_MARKS, SUM(marks) AS TOTAL_MARKS, AVG(marks) AS AVERAGE_MARKS FROM student. This command provides all required aggregate calculations in one query .

The SQL command for this task would be: SELECT UPPER(pname) AS 'patient_name', YEAR(admitdate) AS 'admit_year' FROM hospital; This command transforms the patient names to uppercase and extracts the year from their admission date .

To find the average marks of students born before April 1, 2005, you would use the following SQL query: SELECT AVG(marks) FROM student2 WHERE dob < '2005-04-01'; This query calculates the average of the marks column for all rows where the date of birth is earlier than April 1, 2005 .

You might also like