0% found this document useful (0 votes)
91 views3 pages

SQL Lab: 15 Questions & Answers

The document contains a practical lab file with 15 SQL questions and their corresponding queries. It covers creating and manipulating a 'Student' table and a 'Course' table, including operations like inserting, updating, deleting records, and retrieving data based on specific conditions. Additionally, it includes aggregate functions to analyze student marks and course information.

Uploaded by

homev71661
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)
91 views3 pages

SQL Lab: 15 Questions & Answers

The document contains a practical lab file with 15 SQL questions and their corresponding queries. It covers creating and manipulating a 'Student' table and a 'Course' table, including operations like inserting, updating, deleting records, and retrieving data based on specific conditions. Additionally, it includes aggregate functions to analyze student marks and course information.

Uploaded by

homev71661
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

SQL Practical Lab File (15 Questions with Answers)

1. Create a table named 'Student' with Roll_No, Name, Course, and Marks.

SQL Query:

CREATE TABLE Student (

Roll_No INT PRIMARY KEY,

Name VARCHAR(50),

Course VARCHAR(30),

Marks INT

);

2. Insert records into the 'Student' table.

SQL Query:

INSERT INTO Student (Roll_No, Name, Course, Marks) VALUES (1, 'Aman', 'BCA', 85);

INSERT INTO Student (Roll_No, Name, Course, Marks) VALUES (2, 'Neha', 'BCA', 90);

INSERT INTO Student (Roll_No, Name, Course, Marks) VALUES (3, 'Rahul', 'BBA', 78);

INSERT INTO Student (Roll_No, Name, Course, Marks) VALUES (4, 'Priya', 'BCA', 88);

INSERT INTO Student (Roll_No, Name, Course, Marks) VALUES (5, 'Sahil', 'BBA', 82);

3. Display all records from 'Student' table.

SQL Query:

SELECT * FROM Student;

4. Display students having marks greater than 80.

SQL Query:

SELECT * FROM Student WHERE Marks > 80;


5. Update marks of student whose Roll_No is 3.

SQL Query:

UPDATE Student SET Marks = 85 WHERE Roll_No = 3;

6. Delete record of student whose Name is 'Sahil'.

SQL Query:

DELETE FROM Student WHERE Name = 'Sahil';

7. Display students in ascending order of Marks.

SQL Query:

SELECT * FROM Student ORDER BY Marks ASC;

8. Count total number of students.

SQL Query:

SELECT COUNT(*) AS Total_Students FROM Student;

9. Find maximum and minimum marks.

SQL Query:

SELECT MAX(Marks) AS Highest_Marks, MIN(Marks) AS Lowest_Marks FROM Student;

10. Display average marks of students.

SQL Query:

SELECT AVG(Marks) AS Average_Marks FROM Student;

11. Display sum of marks of all students.


SQL Query:

SELECT SUM(Marks) AS Total_Marks FROM Student;

12. Create a table named 'Course'.

SQL Query:

CREATE TABLE Course (

Course_ID INT PRIMARY KEY,

Course_Name VARCHAR(30),

Duration VARCHAR(10)

);

13. Insert records into 'Course' table.

SQL Query:

INSERT INTO Course (Course_ID, Course_Name, Duration) VALUES (101, 'BCA', '3 Years');

INSERT INTO Course (Course_ID, Course_Name, Duration) VALUES (102, 'BBA', '3 Years');

INSERT INTO Course (Course_ID, Course_Name, Duration) VALUES (103, 'MBA', '2 Years');

14. Display all records from 'Course' table.

SQL Query:

SELECT * FROM Course;

15. Display students enrolled in 'BCA' course.

SQL Query:

SELECT * FROM Student WHERE Course = 'BCA';

Common questions

Powered by AI

Joining tables in SQL is done using a SELECT statement that includes a JOIN clause to combine rows from two or more tables, based on a related column. For 'Student' and 'Course' tables, an example query would be: SELECT Student.Name, Course.Course_Name FROM Student INNER JOIN Course ON Student.Course = Course.Course_Name. This retrieves student names along with their course names. Such joins are useful in relational databases as they allow for data to be stored separately (minimizing redundancy) but queried together, enabling complex queries that can utilize relationships between different data entities .

Dynamically growing datasets can introduce challenges such as performance degradation, data redundancy, and increased complexity in database queries. Performance issues arise from larger volumes of data causing slower query responses. Data redundancy can occur due to inconsistent data entry practices. These challenges can be mitigated through normalization to reduce redundancy, implementing indexing to speed up query responses, and regularly analyzing usage patterns to optimize query performance. Additionally, partitioning large tables and maintaining regular backups can help manage data efficiently, ensuring scalable and robust database management .

One might choose a DELETE operation over TRUNCATE because DELETE allows for the removal of specific records based on conditions, providing greater precision and control. DELETE operations can also be used with transaction logging and constraints, allowing rollback if necessary, while TRUNCATE removes all records indiscriminately and cannot be used with WHERE. This makes DELETE suitable for situations where data needs to be selectively purged or in environments where preserving a transaction history is critical. DELETE ensures flexibility and data integrity, critical in nuanced data management scenarios .

To update multiple records in a table, you would use an SQL query with conditions that target the specific rows needing updates. For example: UPDATE Student SET Marks = Marks + 5 WHERE Course = 'BCA'. This query increases the marks of all students enrolled in the BCA course by 5 points. Such an update might be necessary to apply a bonus to particular groups or correct data for a specific category while maintaining the integrity of the dataset by not altering records outside the specified conditions .

To retrieve the names and courses of students who have achieved more than 80 marks, the following SQL query can be used: SELECT Name, Course FROM Student WHERE Marks > 80. This query filters the 'Student' table to return only rows where the 'Marks' field is greater than 80, and selects the 'Name' and 'Course' columns for those rows .

Best practices for writing complex SQL queries include formatting SQL statements with indentation and line breaks to improve readability, using descriptive alias names for tables and columns, commenting on code segments to explain complex logic, and breaking down large queries into smaller, reusable views or subqueries. Structuring SQL commands to follow a logical order—SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY—enhances clarity. Avoiding the use of SELECT * and explicitly listing columns minimizes ambiguity. These practices help maintain clean, understandable, and reusable SQL code, crucial for long-term database management and collaboration .

Primary keys are crucial in database tables because they ensure each record is unique and can be reliably referenced. They enforce data integrity by preventing duplicate entries and ensuring that each record can be uniquely identified, facilitating accurate and efficient data retrieval, updates, and relationship building between tables through foreign keys. For example, in the 'Student' table, 'Roll_No' is a primary key, which ensures that each student has a unique identifier, preventing potential data conflicts and ensuring that operations like updates or deletes affect only the intended records .

A 'LEFT JOIN' might be preferred over an 'INNER JOIN' when you need to retrieve all records from the left table, along with matched records from the right table, and to include unmatched records from the left table with NULLs for the right table's columns. This is useful in scenarios where you need to ensure that all data from the primary table is preserved for reporting or analysis, regardless of whether there are matches in the related table. The resulting dataset from a LEFT JOIN includes potentially more records, retaining all entries from the left table and possibly revealing gaps in the right table's data that require investigation .

To calculate the mean (average) using SQL, you would use the AVG() function, e.g., SELECT AVG(Marks) AS Average_Marks FROM Student. SQL does not directly support a median function, but it can be computed via a subquery that orders the data and selects the middle value(s). For mode, which is the most frequently occurring value, you might use GROUP BY and ORDER BY with LIMIT to identify the most common value, though SQL doesn't have a direct mode function either. These calculations are essential in data analysis to provide insights into the distribution and central tendency of the data, facilitating better understanding, and decision-making based on patterns observed within the dataset .

Ensuring data consistency and accuracy in interconnected tables involves using foreign keys to enforce referential integrity, maintaining consistent data types and formats across related fields, and applying constraints like UNIQUE and NOT NULL to prevent invalid data entries. Transactions with COMMIT and ROLLBACK help ensure atomicity, where either all operations within a transaction are completed successfully or none at all, preserving consistent database states. Additionally, using triggers and stored procedures can automate checks and updates across related tables, enhancing consistency and accuracy throughout operations .

You might also like