0% found this document useful (0 votes)
8 views2 pages

SQL Queries for Student Database Management

The document provides SQL commands to create tables for students and their results, including constraints like primary and foreign keys. It also includes sample data for both tables and various SQL queries to retrieve specific information, such as total marks for students in a particular class, city-wise student counts, and students with names starting with 'V'. Additionally, it demonstrates how to update the results table with a total score calculated from individual marks.

Uploaded by

22bca04
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

SQL Queries for Student Database Management

The document provides SQL commands to create tables for students and their results, including constraints like primary and foreign keys. It also includes sample data for both tables and various SQL queries to retrieve specific information, such as total marks for students in a particular class, city-wise student counts, and students with names starting with 'V'. Additionally, it demonstrates how to update the results table with a total score calculated from individual marks.

Uploaded by

22bca04
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Questions:-

student(roll_no,name,city,pin_code,class)
result(roll_no,m1,m2,m3)

q-1. Write Sql to create table for approprite constraints.

create table student


2 (
3 roll_no int primary key,
4 name varchar(10),
5 city varchar(12),
6 pin_code int
7 );

Data:
-------
ROLL_NO NAME CITY PIN_CODE CLASS
---------- ---------- ------------ ---------- ------
1 OM Bardoli 394350 FYBCA
2 SAI Bardoli 394350 FYBBA
3 RAM Sarbhon 394601 FYBCA
4 JAI Sarbhon 394601 SYBBA
5 SHREE Vyara 394701 SYBCA
6 Siya Surat 394301 SYBBA
7 Riya Vadodra 494301 SYCBA
8 Vikas Vadodra 494301 TYBBA
9 Vishal Vadodra 494301 TYBCA
10 Dipesh Rajkot 494301 TYBCA

create table result


2 (
3 roll_no int primary key,
4 m1 int,
5 m2 int,
6 m3 int,
7 foreign key(roll_no) refernces student(roll_no)
8 );

alter table result add total int;


update result set total=m1+m2+m3;

Data:
--------

ROLL_NO M1 M2 M3 TOTAL
---------- ---------- ---------- ---------- ----------
1 45 60 57 162
2 97 95 89 281
3 60 62 75 197
4 60 42 55 157
5 35 25 15 75
6 15 15 15 45
7 45 56 50 151
8 80 82 75 237
9 62 64 70 196
10 44 64 50 158

Q-2: List name and total marks of all th students who are study in class FYBCA.
select [Link],[Link]
from student s join result r
on s.roll_no=r.roll_no
where [Link]='FYBCA';

Q-3. Display City Wise total total number of student.


select city,count(*) from student group by city;

Q-4. List The Name of students who scored more than 60% in mark2.
select [Link],r.m2
from student s
join result r
on s.roll_no=r.roll_no
where r.m2>r.m2*0.6;

Q-5. List out the name of student whose name start with 'v'.
select name from student where name like 'v%' or name like 'V%';

Common questions

Powered by AI

The JOIN operation in SQL allows for combining rows from two or more tables based on a related column. In the query example where students' names and total marks were selected, the JOIN operation is executed with: FROM student s JOIN result r ON s.roll_no = r.roll_no. This joins the 'student' and 'result' tables using their common 'roll_no' field, allowing for retrieval of combined data .

To list the names and total marks of students in the 'FYBCA' class, the SQL query is: SELECT s.name, r.total FROM student s JOIN result r ON s.roll_no = r.roll_no WHERE s.class = 'FYBCA'. This join operation combines the 'student' and 'result' tables to provide the required information .

The SQL statements for creating and altering tables ensure data completeness and accuracy by defining constraints and data types explicitly. For example, specifying columns and their data types in CREATE TABLE ensures that only valid data types are stored. Constraints like PRIMARY KEY ensure uniqueness and identification of records. Adding columns with ALTER TABLE, such as the 'total' field in the 'result' table, allows for calculated data storage and bolsters data accuracy by systematically updating dependent fields .

Using the LIKE operator in SQL, especially without anchored patterns, can be inefficient as it may result in a full table scan if indexes are not optimally used. In the query SELECT name FROM student WHERE name LIKE 'V%' OR name LIKE 'v%', it checks for any name starting with 'V' or 'v', potentially scanning every row if indexes are not leveraged properly. Additionally, it can be case-sensitive depending on collation settings, potentially missing some results if both cases aren't considered .

A FOREIGN KEY constraint in the 'result' table is necessary to maintain data integrity and to create a relationship between the 'result' and 'student' tables. It ensures that every roll_no in the 'result' table corresponds to a valid roll_no in the 'student' table, prohibiting orphaned records in the 'result' table and ensuring referential integrity .

To list students whose name starts with 'V', you can use the SQL query: SELECT name FROM student WHERE name LIKE 'V%' OR name LIKE 'v%'. This uses the LIKE operator to match names starting with either uppercase or lowercase 'V' .

The SQL statement to calculate the total marks for each student is: UPDATE result SET total = m1 + m2 + m3. This statement computes the sum of marks m1, m2, and m3 for each student and stores it in the 'total' column .

To find students who scored more than 60% in the second mark (m2), use the following SQL query: SELECT s.name, r.m2 FROM student s JOIN result r ON s.roll_no = r.roll_no WHERE r.m2 > 60. Note: The provided condition is incorrect as r.m2 should be compared to a real threshold (like 60) instead of a relative calculation .

The SQL command to display the number of students per city is: SELECT city, COUNT(*) FROM student GROUP BY city. This query groups the data by 'city' and counts the number of students in each group .

To create the 'student' table with appropriate constraints, the SQL statement should define the primary key and set the data types as specified: CREATE TABLE student ( roll_no INT PRIMARY KEY, name VARCHAR(10), city VARCHAR(12), pin_code INT ). This sets 'roll_no' as the primary key and specifies data types for each column .

You might also like