Unit 3
Unit 3
1. What is the difference between the SELECT and PROJECT operations in SQL?
📘 Example:
📘 Example:
It retrieves only those students whose marks are greater than 80.
Arithmetic operations in SQL perform calculations like addition (+), subtraction (-),
multiplication (*), division (/).
📘 Example:
FROM Student;
4. Explain how logical operations (AND, OR, NOT) are used in SQL queries.
📘 Example:
5. What is the purpose of SQL functions for date and time? Provide an example.
SQL Date and Time functions are used to extract, modify, or calculate date and time
information.
📘 Example:
SELECT CURRENT_DATE;
Another example:
6. What are integrity constraints in SQL? Provide examples of primary key and foreign key
constraints.
📘 Examples:
GROUP BY groups rows that have the same values into summary rows (like totals,
averages).
📘 Example:
FROM Student
GROUP BY Class;
📘 Example:
Types of Joins:
📘 Example:
UNION
📘 11. How Are Arithmetic and Logical Operations Used in SQL Queries? Illustrate with
Examples 🎯
📘 Common Operators:
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Modulus (%)
FROM Student;
Adds 5 marks to each student.
FROM Employee;
📘 Common Operators:
AND
OR
NOT
📘 Example 1: AND
📘 Example 2: OR
Fetch students who either have marks above 90 or are in class 12th.
📘 Example 3: NOT
✅ Conclusion:
Arithmetic and logical operations make SQL queries powerful by allowing calculations and
filtering based on multiple conditions.
📘 12. Discuss Different Types of SQL Functions (Date and Time, Numeric, String Conversion).
Provide Examples for Each Type 🎯
SQL functions are predefined operations that perform tasks like calculations, formatting, and
processing data.
📘 Examples:
SELECT CURRENT_DATE;
YEAR() → Extracts year from a date.
SELECT NOW();
2. Numeric Functions 🔢
📘 Examples:
Result: 123.46
SELECT ABS(-10);
Result: 10
Result: 8
3. String Functions 📝
📘 Examples:
FROM Employee;
📘 Quick Table:
✅ Conclusion:
SQL functions save time and reduce query complexity by performing common operations
directly inside SQL commands.
📘 13. Explain the Process of Creating Tables with Relationships. How Are Keys (Primary and
Foreign) and Integrity Constraints Implemented in SQL? Provide an Example 🎯
In relational databases, relationships between tables are created using Primary Keys and
Foreign Keys.
Foreign Key connects two tables by referencing the Primary Key of another table.
Integrity constraints like NOT NULL, UNIQUE, CHECK, etc., ensure that the data stored is correct
and consistent.
Class VARCHAR(20)
);
RollNo INT,
Subject VARCHAR(50),
);
In a Library System:
These links ensure that a Borrow record cannot exist unless both the Book and Borrower exist.
Foreign Key Create link between tables RollNo in Marks refers to Student
✅ Conclusion:
Creating tables with proper Primary and Foreign keys and integrity constraints ensures that the
database is accurate, consistent, and logically connected.
📘 14. Explain Nested Queries and Subqueries in SQL. How Do They Differ? Provide Examples
to Demonstrate Their Usage 🎯
📘 Real-life example:
First find the highest scorer (subquery), then find their details (main query).
🔹 Types of Subqueries:
1. Single-Row Subquery
📘 Example: Find students whose marks are greater than the average marks.
SELECT Name
FROM Student
2. Multi-Row Subquery
SELECT Name
FROM Student
📘 Quick Notes:
✅ Conclusion:
Subqueries (Nested Queries) help to break down complex queries and solve them step-by-step.
They are a powerful tool to simplify difficult SQL operations.
📘 15. Explain How the GROUP BY Clause is Used in SQL. Discuss Its Significance in Aggregation
Operations. Provide an Example with Aggregate Functions Like COUNT, SUM, AVG 🎯
It is mostly used with aggregate functions like COUNT, SUM, AVG, MAX, MIN.
📘 Real-life example:
Find the total marks obtained by students class-wise.
🔹 Why is GROUP BY Important?
It summarizes data.
Helps in generating reports like sales per region, marks per class, total salary per
department.
Function Use
📘 Example Query:
FROM Student
GROUP BY Class;
📘 Another Example:
FROM Student
GROUP BY Class;
COUNT() gives how many students are there in each class.
SELECT columns must appear in GROUP BY if they are not used in aggregate functions.
GROUP BY must come after WHERE and before ORDER BY in SQL query structure.
✅ Conclusion:
GROUP BY is used to summarize and analyze large amounts of data by grouping rows based on
one or more columns and applying aggregate functions to each group.
📘 16. Write an SQL Query That Uses the GROUP BY Clause to Find the Total Sales for Each
Product Category 🎯
🧠 Problem Description:
We need to calculate the total sales amount for each product category.
This is a typical use case of GROUP BY with SUM() function.
1 Electronics 5000
2 Furniture 3000
3 Electronics 4500
SaleID ProductCategory Amount
4 Clothing 1500
5 Furniture 2000
FROM Sales
GROUP BY ProductCategory;
📘 Output:
ProductCategory TotalSales
Electronics 9500
Furniture 5000
Clothing 1500
📘 Explanation:
GROUP BY ensures that totals are calculated separately for Electronics, Furniture, and
Clothing.
✅ Conclusion:
Using GROUP BY with SUM() allows you to summarize sales data and analyze business
performance by product categories or any other grouping factors.
Continuing immediately with next:
📘 17. Discuss the Different Types of Joins in SQL (INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL
JOIN) with Examples 🎯
Joins are used in SQL to combine data from two or more tables based on related
columns.
📘 Real-life example:
In a school database, you can join the Student table and Marks table using the RollNo field to
get complete information about each student.
🔹 Types of Joins:
1. INNER JOIN 🔗
📘 Example:
FROM Student
ON Student.RollNo = Marks.RollNo;
Shows only students who have matching records in both Student and Marks tables.
2. LEFT JOIN 🔗
Returns all rows from the left table and matching rows from the right table.
If no match, NULL is shown for right table columns.
📘 Example:
FROM Student
ON Student.RollNo = Marks.RollNo;
3. RIGHT JOIN 🔗
Returns all rows from the right table and matching rows from the left table.
📘 Example:
FROM Student
ON Student.RollNo = Marks.RollNo;
4. FULL JOIN 🔗
📘 Example:
FROM Student
ON Student.RollNo = Marks.RollNo;
Shows all students and all marks, whether or not they match.
✅ Conclusion:
Joins allow you to combine data from multiple tables to create complete, meaningful reports
based on relationships between tables.
📘 18. What Are Relational Set Operations in SQL? Explain UNION, INTERSECT, and EXCEPT
Operations with Examples 🎯
The queries must have the same number of columns with compatible data types.
📘 Real-life example:
Combining lists of students from two different classes into one list without duplicates.
📘 Example:
UNION
2. INTERSECT ➗
📘 Example:
INTERSECT
Returns rows from the first query that are not in the second query.
📘 Example:
EXCEPT
Column order and data types must match between the two queries.
📘 Quick Table:
UNION Combine and remove duplicates All unique students from two classes
EXCEPT Rows in first query, not in second Students in Class10 but not in SportsTeam
✅ Conclusion:
Relational set operations like UNION, INTERSECT, and EXCEPT are powerful tools to combine,
compare, and filter results from multiple queries easily.
✅✅✅
Now all Unit 3 — 2 Marks and 10 Marks questions completed fully and properly!
If ready, I can immediately start Unit 4 — 2 Marks Questions in the same style.
Say Start Unit 4 ✅ if you want to continue!