0% found this document useful (0 votes)
50 views4 pages

SQL Question Bank: Student Table Queries

The document provides a comprehensive SQL question bank focused on basic SQL operations using a 'student' table. It includes queries for creating the table, inserting data, selecting and filtering records, using aggregate functions, modifying table structure, updating and deleting records, and miscellaneous SQL functions. Each section contains specific SQL queries along with their expected outputs for various tasks related to the student data.

Uploaded by

maheshbsawant610
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)
50 views4 pages

SQL Question Bank: Student Table Queries

The document provides a comprehensive SQL question bank focused on basic SQL operations using a 'student' table. It includes queries for creating the table, inserting data, selecting and filtering records, using aggregate functions, modifying table structure, updating and deleting records, and miscellaneous SQL functions. Each section contains specific SQL queries along with their expected outputs for various tasks related to the student data.

Uploaded by

maheshbsawant610
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

Question Bank - Basic [SQL] Consider a example table as -

step 1 - shoot following query to create a tablestep

*** CREATE TABLE student (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50), age
INT, email VARCHAR(100), gender VARCHAR(10), grade CHAR(1),marks INT, city
VARCHAR(50),registration_date DATE);

2 - Shoot following query to insert data in table

*** INSERT INTO student (name, age, email, gender, grade, marks, city,
registration_date) VALUES
('Alice', 20, 'alice@[Link]', 'Female', 'A', 88,
'Delhi', '2024-06-01'),
('Bob', 21, 'bob@[Link]', 'Male', 'B', 75, 'Mumbai',
'2024-05-21'),
('Carol', 22, 'carol@[Link]', 'Female', 'C', 64,
'Chennai', '2024-03-14'),
('David', 19, 'david@[Link]', 'Male', 'A', 92, 'Pune',
'2024-04-10'),
('Eva', 23, 'eva@[Link]', 'Female', 'B', 78, 'Delhi',
'2024-06-10'),
('Frank', 20, 'frank@[Link]', 'Male', 'C', 55,
'Bangalore', '2024-01-29'),
('Grace', 18, 'grace@[Link]', 'Female', 'A', 95,
'Mumbai', '2024-05-05'),
('Harry', 21, 'harry@[Link]', 'Male', 'B', 70,
'Kolkata', '2024-06-01'),
('Ivy', 20, 'ivy@[Link]', 'Female', 'C', 60,
'Hyderabad', '2024-02-15'),
('Jack', 22, 'jack@[Link]', 'Male', 'A', 85, 'Delhi',
'2024-03-20'),
('Kathy', 24, 'kathy@[Link]', 'Female', 'B', 77,
'Chennai', '2024-04-25'),
('Liam', 19, 'liam@[Link]', 'Male', 'C', 58, 'Pune',
'2024-01-01'),
('Mona', 21, 'mona@[Link]', 'Female', 'A', 90,
'Delhi', '2024-05-30'),
('Nate', 20, 'nate@[Link]', 'Male', 'B', 74,
'Bangalore', '2024-03-15'),
('Olivia', 22, 'olivia@[Link]', 'Female', 'C', 69,
'Kolkata', '2024-04-12'),
('Paul', 23, 'paul@[Link]', 'Male', 'A', 83, 'Mumbai',
'2024-06-18'),
('Quinn', 18, 'quinn@[Link]', 'Female', 'B', 72,
'Hyderabad', '2024-05-22'),
('Rita', 20, NULL, 'Female', 'C', 65, 'Chennai', '2024-03-
01'),
('Steve', 21, 'steve@[Link]', 'Male', 'A', 88,
'Delhi', '2024-06-24'),
('Tina', 19, 'tina@[Link]', 'Female', 'B', 76, 'Pune',
'2024-04-02');

✅ Basic SQL Questions (Single Table)

🔹 SELECT & FILTERING (Q1–Q15)


1. Select all columns from the student table.
****Answer/SQL Query === select * from student;
2. Select only names and ages of students.
****Answer/SQL Query === select name , age from student;
3. Find students who are older than 18.
****Answer/SQL Query === select * from student where age >18;
4. Find students who are exactly 20 years old.
****Answer/SQL Query === select * from Student where age =20;
5. Find students with marks greater than or equal to 75.
****Answer/SQL Query === SELECT * FROM student WHERE marks >= 75;
6. Find students living in 'Delhi'.
****Answer/SQL Query === select * from student where city='delhi';
7. Find female students.
****Answer/SQL Query === select * from student where gender='female';
8. Find students whose name starts with 'A'.
****Answer/SQL Query === select * from student where name like 'A%';
9. Find students whose name ends with 'n'.
****Answer/SQL Query === select * from student where name like '%n';
[Link] students whose name contains 'an'.
****Answer/SQL Query === select * from student where name like '%an%';
[Link] students whose marks are between 60 and 80.
****Answer/SQL Query === select * from student where marks between 60 and 80;
[Link] students whose city is either 'Delhi', 'Mumbai', or 'Chennai'.
****Answer/SQL Query === select * from student where city IN
('delhi','mumbai','chennai');
[Link] students who are not from 'Pune'.
****Answer/SQL Query === select * from student where city != 'pune'; OR select *
from
student where city not in ('pune');
[Link] students who are not female.
****Answer/SQL Query === select * from student where gender != 'female';
[Link] students with null emails.
****Answer/SQL Query === select * from student where email is null;

🔹ORDER BY, LIMIT (Q16–Q20)

[Link] all students sorted by marks in descending order.


****Answer/SQL Query === select * from student order by marks desc;
[Link] top 5 students with highest marks.
****Answer/SQL Query === select * from student order by marks desc limit 5;
[Link] bottom 3 students with lowest marks.
****Answer/SQL Query === select * from student order by marks asc limit 3;
[Link] students ordered by name alphabetically.
****Answer/SQL Query === select * from student order by name desc;
[Link] students registered most recently (latest date first).
****Answer/SQL Query === select * from student order by registration_date desc;

🔹AGGREGATE FUNCTIONS (Q21–Q30)

[Link] total number of students. ****Answer/SQL Query


=== select count(*) from student;
[Link] the average age of students. ****Answer/SQL Query
=== select avg(age) from student;
[Link] the maximum marks. ****Answer/SQL Query
=== select max(marks) from student;
[Link] the minimum age. ****Answer/SQL Query
=== select min(age) from student;
[Link] the total marks of all students. ****Answer/SQL Query
=== select sum(marks) from student;
[Link] how many students are from 'Delhi'. ****Answer/SQL Query
=== select count(*) from student where city = 'delhi';
[Link] male and female students. ****Answer/SQL Query
=== select gender,count(*) from student GROUP BY gender;
[Link] average marks for each grade. ****Answer/SQL Query
=== select grade,avg(marks) AS average_marks from student group by grade
[Link] number of students in each city. **Answer/SQL Query===
select city,count(*) AS Number_OF_Student_each_City from student group by city;
[Link] max, min, and avg marks. ****Answer/SQL Query
=== select max(marks),avg(marks),min(marks) from student;

🔹GROUP BY & HAVING (Q31–Q35)

[Link] students by age. ****Answer/SQL


Query=== select age,count(*) from student group by age;
[Link] cities with more than 3 students. ****Answer/SQL
Query=== select city,count(*) from student group by city having count(*) >3;
[Link] grades with average marks above 80. ****Answer/SQL
Query=== select grade,avg(marks) from student group by grade having avg(marks) >
80;
[Link] all genders with at least 2 students. ****Answer/SQL
Query=== select gender,count(*) from student group by gender having count(*) >= 2;
[Link] ages with more than 1 student. ****Answer/SQL
Query=== select age,count(*) from student group by age having count(*) > 1;

🔹MODIFY TABLE STRUCTURE (Q36–Q40)

[Link] a new column phone to the student table.


****Answer/SQL Query === alter table student add Phone bigint;
[Link] the grade column from CHAR(1) to VARCHAR(2).
****Answer/SQL Query === alter table student modify grade VARCHAR(2);
[Link] the marks column to score.
****Answer/SQL Query === alter table student change marks Score int;
[Link] the column email.
****Answer/SQL Query === alter table student drop column email;
[Link] default value for city as 'Unknown'.
****Answer/SQL Query === alter table student alter column city set default
'Unknown';

🔹UPDATE & DELETE (Q41–Q45)

[Link] marks to 100 for student named 'John'.


****Answer/SQL Query === update student set score = 100 where name = 'bob';
[Link] all student marks by 5.
****Answer/SQL Query === update student set score = score +5;
[Link] city to 'Mumbai' for all students with null ci
****Answer/SQL Query === update student set city = 'Mumbai' where city is null;
[Link] all students with marks less than 35.
****Answer/SQL Query === DELETE FROM student WHERE score < 35;
[Link] students from 'Chennai'.
****Answer/SQL Query === delete from student where city = 'Chennai';

🔹MISCELLANEOUS (Q46–Q50)

[Link] current date in SQL.


****Answer/SQL Query === select curdate() as today;
[Link] students registered in year 2024.
****Answer/SQL Query === SELECT * FROM student WHERE YEAR(registration_date) =
2024;
[Link] length of each student’s name.
****Answer/SQL Query === SELECT name, CHAR_LENGTH(name) FROM student;
[Link] student names to uppercase.
****Answer/SQL Query === SELECT name, UPPER(name) FROM student;
[Link] names with ‘Mr.’ prefix (use CONCAT).
****Answer/SQL Query === SELECT name, CONCAT('Mr. ', name) FROM student;

Document by: MR__SAWANT__46

Common questions

Powered by AI

To accommodate a new requirement for storing phone numbers in the 'student' table, you would need to add a new column using the SQL command: ALTER TABLE student ADD Phone BIGINT. This type of modification is important in SQL databases because it allows for the dynamic adaptation of the table structure to meet changing data requirements, ensuring that the database remains useful and relevant as new data fields become necessary .

SQL allows for precise updates to records, ensuring database accuracy and integrity. For instance, if a student's marks need increasing due to an error, you can use: UPDATE student SET score = score + 5 WHERE name = 'Bob'. This ensures corrections are made systematically without affecting unrelated data. However, careless updates can introduce errors; thus, conditional updates should be carefully crafted to suit only the needed records .

Setting a default value for a column, like the 'city' column in the 'student' table to 'Unknown', ensures that all new records will have a defined value for that column even if not explicitly specified during data entry. This can enhance data integrity by preventing null values where they are not meaningful. However, it may also mask instances where a city has not been provided when it should be, potentially misleading if analyzed without caution .

NULL values in SQL represent unknown or missing data, crucial for accurate analyses. In a student database, detecting NULL emails via SELECT * FROM student WHERE email IS NULL is critical for maintaining communication lines. While NULL signifies absent data that may need rectification, it can also complicate computing averages or aggregate results if not appropriately managed. Solutions include assigning default values or requiring compulsory data entry for critical fields .

The 'GROUP BY' clause in SQL is used to organize results into groups based on one or more columns, facilitating aggregate computations on subsets of data. The 'HAVING' clause further refines these groups based on conditions applied to aggregate functions. For instance, to find cities with more than 3 students, you use: SELECT city, COUNT(*) FROM student GROUP BY city HAVING COUNT(*) > 3. This ensures that the database queries not only aggregate data but also filter these aggregates based on specific criteria, which is vital in analyzing patterns or distributions, such as student demographics .

SQL queries are instrumental in managing and analyzing student demographics, providing insights like age distributions (SELECT age, COUNT(*) FROM student GROUP BY age) or city-based student counts. Such data can inform resource allocation or targeted interventions. However, limitations include the relational database requirement for predefined structured data, which may not accommodate unstructured or real-time inputs effectively, potentially limiting dynamic analysis capabilities .

SQL transformations like CONCAT enable customized data presentation without altering underlying data, enhancing user comprehension. For example, SELECT name, CONCAT('Mr. ', name) FROM student adds a salutation, improving formality or clarity for reports. Such transformations facilitate tailored outputs aligning with user requirements or preferences, potentially improving decision-making processes when data formats are critical .

SQL DELETE queries permanently remove records from a database, so they must be used cautiously to avoid data loss. For example, DELETE FROM student WHERE city = 'Chennai' would erase all Chennai students. While necessary for removing irrelevant or incorrect data, this action is irreversible and could remove important records if not executed with precise conditions. Implementing preliminary checks, backups, or transaction functions can prevent unintentional data loss .

SQL aggregate functions like COUNT, AVG, MAX, and SUM are powerful tools for summarizing and analyzing data in databases. In educational settings, they can be used to evaluate overall student performance, such as finding average marks or determining maximum marks in a cohort (e.g., SELECT AVG(marks) FROM student). However, they must be used carefully to ensure that the data context (e.g., class sizes, marking schemes) is considered, as aggregate summaries can obscure individual performance variations .

Using ORDER BY with LIMIT in SQL queries sorts the results and restricts them to a specified number of records, which is beneficial for managing large datasets by improving query performance and focusing on relevant results, like listing top-performing students (e.g., SELECT * FROM student ORDER BY marks DESC LIMIT 5). However, drawbacks include potential overlooking of significant records not included within the limit, which could lead to incomplete analyses or biased conclusions .

You might also like