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

Deepika Mysql

SQL file

Uploaded by

preeti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
9 views

Deepika Mysql

SQL file

Uploaded by

preeti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 16
Practical MySQL PRATICAL 1 Problem statement: Create a students table with the student id, name, and marks as attributes ‘where the student id is the primary key. Solution: To cteate a student table with studid, name, and marks as attributes in MySQL, where student_id is the primary key, you can use the following SQL statement: Source Code: create table student {studid int primary key, narne varchar(30), marks int hi forte eee aa eu (studid int primary key, name varchar(30), Etats -> ); Query OK, © rows affected (0.02 PRATICAL 2 Problem statement: In the table ‘student’ created in practical 26, insert the details of new students. Solution: This command inserts 10 rows into the student table, where each student has a unique student_id, aname, anda marks value. Make sure that student_id values are unique and that they follow the primary key constraints. Source Code: INSERT INTO student (studid, name, marks} VALUES (1, Rahul Sharma; 85), (2, ‘Priya Singh’, 78), (3, Amit Patet, 82), (4, Sneha Gupta’. 88), (5, ‘Rohan Das’, 74), (6, ‘Anjali Mehta’, 90), (7, "Vikram Rao, 80), (8, Pooja Nair’. 76) (9, Karan Verma’ 81), (10, "Neha Joshi, 83); mysql> INSERT INTO student (studid, name, marks) VALUES -> (1, ‘Rahul Sharma', 85), @, ‘Priya Singh', 78), cee Urea ey (4, ‘Sneha Gupta’, 88), (5, ‘Rohan Das', 74), (6, ‘Anjali Mehta! , 99), (7, ‘Vikram Rao', 89), (8, ‘Pooja Nair’, 76), (9, ‘Karan Verma’, 81), (10, "Neha Joshi', 89); Query OK, 10 rows affected (0.01 sec) Records: 10 Duplicates: © Warnings: PRATICAL 3 Problem statement: ‘Write SQL command to get the details of the students with marks more than 80. Solution: This command selects all columns (*) from the student table where the marks column has a value greater than 80. It will return the studid, name, and marks of each student who meets this condition, Source Code: SELECT * FROM student WHERE marks > 80; mysql> SELECT * FROM student rai i,t MPa | 7 EU Pe) Sharma vba tee Sneha Gupta rOPeiemn ote Karan Verma Neha Joshi rows in set (0.00 sec) PRATICAL 4 Problem statement: ‘Write SQL command to Find the min, max, sum, and average of the marks in a student marks table, Solution: + min_marks: The minimum marks in the table. * max_marks: The maximum marks in the table. ‘+ total_marks: The sum of all marks. * average_marks: The average marks of all students, Each of these values will be calculated from the marks column in the student table. Source Code: SELECT MIN(marks) AS min_marks, MAX(marks) AS max_marks, SUM(marks) AS total_marks, AVG(marks) AS average_marks FROM student; een MIN(marks) Cees te MAX (marks) Cea baa SUM(marks) A Mee Cea co Macrae CEC EarT Ts [a-) eesate cits + oe one on row in set (0.00 sec) PRATICAL 5 Problem statement: Delete the details of a student table created in Practical 26. Solution: The DELETE FROM student; command clears all data from the student table but keeps the table structure. The DROP TABLE student; command permanently removes the table and all its contents from the database. Use this with caution, as it cannot be undone. Source Code DELETE FROM student; DROP TABLE student; PRATICAL 6 Problem statement: Find the total number of customers from each country in the table (customer ID, customer Name, country) using group by. Solution: This command groups the data by country and counts the number of customer_id entries in each group, resulting in the total number of customers per country. The output will show each country with the corresponding count of customers. Source Code: SELECT country, COUNT(customer_id) AS total_custorners FROM customer GROUP BY country; \> SELECT country, COUNT(customer_id) AS total_customers Sema Seco ake Cos eye India| To | Canada | UK | rows in set (0.00 sec) PRATICAL 7 Problem statement: ‘Write a SQL query to order the (student ID, marks) table in descending order of the marks. Solution: This command will display the studid and marks columns, sorted in descending order based on the marks column, with the highest marks appearing first. Source Code: SELECT studid, marks FROM student ‘ORDER BY marks DESC; mysql> SELECT studid, marks pees mets Lk -> ORDER BY marks DESC; 7 7 studid | marks | | | | | | | | | | | + 10 rows in set (0.00 sec) PRATICAL 8 Problem statement: For the given table ‘Hospital’ write SQL command to display name all patient admitted in month of May. PID [PNAME [apmiTpate | DepT__| Fees ‘AP/PT/O01 | Arjun Verma [21-04-2019 | ENT 250 AP/PT/O02 | NeelamReddy | 12-05-2019 | Cardio 400 ‘AP/PT/003 | Kavita Singh [19-05-2019 | Cardio 400 APIPT/ON4 | Vikram Patel _| 24-06-2019 | Neuro 600 Solution: To display the names of all patients admitted in the month of May from the Hospital table, you can use the following SQL query. This query assumes the ADMITDATE is stored as a date in the DD/MM/YYYY format (or similar) and filters the records where the admission date falls in May. Source Code: SELECT PNAME FROM Hospital WHERE MONTH(ADMITDATE) mysql> SELECT PNAME es aCe ae Psa ea US eas ak at + Neelam Reddy | Kavita Singh | = rows in set (0.00 sec) PRATICAL 9 Problem statement: for the given table ‘Hospital’ write SQL command to Display patient name in upper case with ‘year of admission, PID AP/PT/OOL AP/PT/OO2 AP/PT/O03 APIPT/OOA Solution: PNAME [Arjun Verma [Neelam Reddy [ Kavita Singh Vikram Patel [ApMimpate | DEPT | FEES [21-04-2019 | ENT 250 [12-05-2019 | Cardio 400 [19-05-2019 | Cardio 400 24-06-2019 | Neuro 600 UPPER(PNAME): Converts the PNAME (patient name) to uppercase. YEAR(ADMITDATE): Extracts the year from the ADMITDATE. Source Code: SELECT UPPER(PNAME) AS patient_name, YEAR(ADMITDATE) AS admission_year FROM Hospital; ean emai Poa sccm VIKRAM PATEL | + eon operant aaa 2019 2019 i 2019 | I 1 2019 | + Taner Cat mre PRATICAL 10 Problem statement: for the given table ‘Hospital’ Create sql query to display first four Letters of the patientname along with length of their name who admitted before may. PID. PNAME ADMITDATE | DEPT | FEES ‘ap/PT/001 | Arjun Verma | 21-04-2019 | ENT 250 AP/PT/002 | Neelam Reddy | 12-05-2019 | Cardio 400 Ap/PT/003 | Kavita Singh [19-05-2019 | Cardio 400 AP/PT/004 | Vikram Patel 24-06-2019 | Neuro 600 Solution: LEFT(PNAME, 4): Extracts the first four Letters of the patient name (PNAME), LENGTH(PNAME): Calculates the length of the patient name. WHERE MONTH(ADMITDATE) <5: Filters to include only patients admitted before May {i.e., months 1 to 4). Source Code: SELECT LEFT(PNAME, 4) AS first_four_letters, LENGTH (PNAME) AS name_length FROM Hospital WHERE MONTH(ADMITDATE) <5; ELECT LEFT( eer ee eae Se Same ay PRATICAL 11 Problem statement: Retrieve the list of students sorted by their marks in descending order. This sorting will facilitate quick identification of high achievers. [ name stream | marks wl | Scienes | 2 | Priya Singh Commerce | 78 | [ 3 | AmitPatet Arts 92 I 4 | Sneha Gupta Science 88 | 5 | Rohan Das Commerce 74 I 6 | Anjali Mehta Arts: 90 I 7 | Vikram Rao Science | 80 | | & | Pooja Nair 9 | Karan Verma 10 | Neha Joshi Science Solution: ORDER BY marks DESC: Sorts the list by the marks column in descending order, so the students with the highest marks appear at the top. Source Code: SELECT studid, name, marks FROM student ORDER BY marks DESC; Sree ager Cee? => FROM student See Veer a afer i tudid cued Anit Patel | PUSreaL ica PRATICAL 12 Problem statement: Generate a list of students sorted first by their stream and then by their marks in descending order. This will atlow for a comparative analysis of performance across different streams { student id | name stream | marks | | Rahul Sharma Science| 85. 2 | Priya Singh Commerce | _78 | | 3 | Amit Patel Arts 92 | 4 | Sneha Gupta Science 88 | 5 | Rohan Das Commerce 74 | 6 | Anjali Mehta Arts I 7 | Vikram Rao Science | Nair I Karan Verma ___ Arts | I [Neha Joshi Science Solution: ORDER BY stream, marks DESC: Sorts the students first by stream (alphabetically) and then within each stream by marks in descending order. Source Code: SELECT studid, name, stream, marks FROM student ORDER BY stream, marks DESC; Sere PRATICAL 13 Problem statement: Retrieve students’ names and their marks, sorting the results by marks using an alias for clarity. This provides a clean and organized view of student performance. student, me stream [-___1 | RanutShamma [Science | Commerce 2 | Amit Patet Arts 4 | Sneha Gupta Science 5 | Rohan Das Commerce 6 | Anjali Menta Arts 7 | Vikram Rao Science 8 | Pooja Nair Commerce | 9 | Karan Verma Arts 10 | Neha Joshi Science Solution: ‘© name AS student_name: Renames name to student_name fora clear, descriptive alias. ‘© marks AS student_marks: Renames marks to student_marks. + ORDER BY student_marks DESC: Sorts the results by marks in descending order to display the highest marks at the top. This query provides a well-organized view of student performance with clear column names. Source Code: SELECT name AS student_name, marks AS student_marks FROM student ‘ORDER BY student_marks DESC; PRATICAL 14 Problem statement: Calculate the average marks for each stream in the students table. This grouping will help identify which streams are performing well and which may need curricular improvements. [student id [ name stream marks [ 1 | Rahul Sharma Science | 85 [ 2 | Priya Singh Commerce 8 | 3 | Amit Patet Arts | 92 | 4 | Sneha Gupta Science 88 [ | Rohan Das Commerce | 74 | 6 | Anjali Mehta ‘Arts 20 | 7_| Vikram Rao Science 80 | | @ | Karan Verma 410 | Neha Joshi Science Solution: + AVG(marks) AS average_marks: Calculates the average marks for each stream and renames it to average_marks for clarity. ‘© GROUP BY stream: Groups the students by stream, so the average is calculated for each stream separately. This query will provide a list of streams along with the average marks, allowing for a comparative analysis of performance across different streams Source Code: SELECT stream, AVG(marks) AS average_marks FROM student GROUP BY stream; corer Cy PRATICAL 15 Determine the number of distinct streams present in the students table. This will provide insights into the diversity of academic programs within the institution. [student id [ name stream | marks I 1.| Rahul Sharma Science | 85 | 2 | Priya Singh Commerce 78 | 3 | Amit Patel Arts | 92 | 4 | Sneha Gupta Science 88 I 5 | Rohan Das Commerce | 74 | 6 | AnjaliMehta Arts 7[VikramRao——«|—Science | 80 | 8 | Pooja Nair Commer: 76 | 9 | Karan Verma _ Arts En I 20 | Nena Joshi Science | a8 | Solution: COUNTIDISTINGT stream): Counts the unique values in the stream column to find the number of distinct streams. AS total_ streams: Renames the result as total_streams for clarity. Source Cade: SELECT COUNT(DISTINCT stream) AS total_streamns FROM student; mysql> SELECT COUNT(DISTINCT stream) AS total_streams FROM student;

You might also like