0% found this document useful (0 votes)
9 views1 page

SQL Practice Questions and Exercises

The document outlines a series of SQL practice questions aimed at creating a database and performing various operations. It includes tasks such as creating tables for houses and students, setting primary and foreign keys, and executing SQL queries to retrieve specific data. The exercises cover data insertion and various retrieval operations based on different conditions and sorting requirements.

Uploaded by

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

SQL Practice Questions and Exercises

The document outlines a series of SQL practice questions aimed at creating a database and performing various operations. It includes tasks such as creating tables for houses and students, setting primary and foreign keys, and executing SQL queries to retrieve specific data. The exercises cover data insertion and various retrieval operations based on different conditions and sorting requirements.

Uploaded by

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

SQL practice questions

1. Create a database with the name of your own choice.


2. Create the following tables with appropriate data types.
a. Houses(house_id, abbreviation, house_name) and
b. Students(std_id, roll_no, name, house_id).
3. Set the appropriate primary keys and required foreign keys on the tables above.
4. Add some relevant data in those tables. Add at least 4 houses and 15 students.
5. Perform the following operations using SQL queries.
a. Retrieve all columns from the students table.
b. Retrieve the name and roll_no columns for all students.
c. Retrieve all columns from the house table.
d. Retrieve the name and house_name of all students and their respective house.
e. Retrieve the std_id, name, and house_name for students who belong to the "GH".
f. Retrieve the names of students who have a roll_no number greater than "50".
g. Retrieve the names of all students sorted by their name in ascending order.
h. Retrieve all students who are not assigned to any house (i.e., house_id is NULL).
i. Retrieve the house_name of all houses that have students assigned to them.
j. Retrieve the std_id and name of students whose name starts with the letter "A".
k. Retrieve all students who are in a house with house_id = 2.
l. Retrieve the total number of students in the students table.
m. Retrieve the distinct house names from the house table.
n. Retrieve the students who belong to either "RH" or "BH".
o. Retrieve all students and sort them by their roll_no in descending order.

Common questions

Powered by AI

Use 'SELECT std_id, name FROM Students WHERE name LIKE 'A%'' to identify students whose names start with 'A'. This query demonstrates SQL's pattern matching capabilities using the LIKE operator and wildcard '%', allowing searches for strings meeting specific criteria .

To sort students by roll number in descending order, use 'SELECT * FROM Students ORDER BY roll_no DESC'. This is useful for listing recent entries or prioritizing top numbers, helpful in scenarios such as assigning priorities or latest student registrations .

Use the SQL query 'SELECT DISTINCT house_name FROM Houses' to retrieve distinct house names. This operation is important for eliminating duplicate results, ensuring each unique house name appears only once, which is crucial for reporting and analysis tasks where uniqueness is desired .

To set up a relational database structure with tables for houses and students, follow these steps: First, create a database. Then, define two tables: 'Houses' with columns (house_id, abbreviation, house_name) and 'Students' with columns (std_id, roll_no, name, house_id). Assign 'house_id' as the primary key for 'Houses' and 'std_id' for 'Students'. Set 'house_id' in 'Students' as a foreign key referencing 'house_id' in 'Houses'. This establishes a relationship between the tables .

To retrieve the name and house_name of all students with their respective houses, use the SQL query: 'SELECT Students.name, Houses.house_name FROM Students JOIN Houses ON Students.house_id = Houses.house_id'. This query joins the 'Students' and 'Houses' tables on the 'house_id' foreign key relationship .

Execute 'SELECT COUNT(*) FROM Students' to retrieve the total number of students. This uses the aggregate function COUNT to analyze the table and return the total count of entries, which is vital for understanding table size and conducting further statistical analysis .

Use 'SELECT * FROM Students WHERE house_id IS NULL' to find students not assigned to any house. This query reveals data anomalies or incomplete entries where the foreign key 'house_id' has not been set, indicating students without house affiliations .

Use the query 'SELECT name FROM Students WHERE roll_no > 50' to find students with roll numbers greater than 50. This query implies filtering student records based on the condition specified in the WHERE clause, i.e., selecting only those students whose 'roll_no' exceeds 50 .

Execute the query 'SELECT name FROM Students ORDER BY name ASC' to sort student names in ascending order. This sorting is useful for organized data presentation and ease of locating specific students alphabetically in applications such as attendance lists or directories .

The query 'SELECT name FROM Students WHERE house_id IN (SELECT house_id FROM Houses WHERE abbreviation = 'RH' OR abbreviation = 'BH')' retrieves students from the specified houses. This conditional logic illustrates SQL's ability to execute complex checks using subqueries and logical operators, crucial for intricate data retrieval tasks involving multiple conditions .

You might also like