What Is A Query in DBMS: ID Name Marks
What Is A Query in DBMS: ID Name Marks
Advantages of SQL
1. Easy to Learn and Use – SQL uses simple English-like commands, making it
beginner-friendly.
2. Fast Data Retrieval – SQL can quickly fetch data, even from large databases.
3. Works with Large Databases – SQL efficiently handles millions of records.
4. Multiple Users Can Access Data at the Same Time – Supports concurrent access by
multiple users.
5. Secure and Reliable – Provides user access control to protect sensitive data.
6. Standardized Language – Works across different database systems like MySQL,
PostgreSQL, and Oracle.
7. Supports Complex Queries – Allows filtering, sorting, grouping, and joining data
from multiple tables.
8. Automates Tasks – Helps schedule backups, updates, and reports automatically.
9. Reduces Data Duplication – Uses normalization rules to prevent duplicate data
storage.
10. Supports Different Data Types – Can store text, numbers, dates, images, and even
videos.
ID Name Marks
1 Alex 85
2 Bob 90
3 Carol 78
Result:
ID Name Marks
1 Alex 85
2 Bob 90
3 Carol 78
👉 This query retrieves all columns and rows from the Students table.
ID Name Marks
1 Alex 85
2 Bob 90
3 Carol 78
4 David 88
1 Alex 95
2 Bob 90
3 Carol 78
4 David 88
ID Name Marks
1 Alex 95
2 Bob 90
4 David 88
==========================================
Nested Subqueries
A subquery is a query inside another query. A nested subquery means a subquery inside
another SQL query. It helps in filtering and retrieving data based on a condition.
How Nested Subqueries Work
ID Name Marks
1 Alex 85
2 Bob 90
3 Carol 78
Final Output
Name
Bob
ID Name Salary
1 Alice 3000
2 Bob 4000
3 Carol 5000
How it Works
Final Output
Name
Carol
2. Views in DBMS
A view is a saved SQL query that behaves like a table but does not store data permanently.
Uses
Instead of writing a query every time to find students with marks above 80, we can create a
view.
Creating a View
Output
Name Marks
Alex 85
Bob 90
Now, we don’t need to rewrite the query. We can just use TopStudents as a virtual table.
Let’s create a view for employees who earn more than 4000.
Output
Name Salary
Carol 5000
Nested Subqueries: A query inside another query. It runs dynamically and helps
filter or process data.
Views: A saved SQL query that behaves like a virtual table for easy data access.
sql
CopyEdit
CREATE VIEW PassedStudents AS
SELECT Name, Marks FROM Students WHERE Marks >= 40;
sql
CopyEdit
SELECT * FROM PassedStudents;
Output:
Name Marks
Name Marks
Alex 85
Bob 90
David 40
sql
CopyEdit
CREATE VIEW EmployeeNames AS
SELECT Name FROM Employees;
Output:
Name
John
Alice
A view can show only necessary columns, hiding the table structure.
For example, a CustomerView can show only customer names and phone numbers.
5. Improves Maintainability 🔒
If we change a table (e.g., add a column), the view still works without affecting users.
6. Enables Faster Query Execution ⚡
If we rename a column in the main table, the view can be updated without affecting users.
A monthly sales report view can be created once and used repeatedly.
A view does not take extra space; it just presents data dynamically.
Ensures all users see the same, updated data without modifying the actual table.
Improves Maintainability Table changes don’t affect views Views still work
Advantage Query Example Output
Supports Logical
View works even after table changes No impact on users
Independence
Simplifies Report
SELECT * FROM MonthlySales; Predefined sales report
Generation
Enhances Data Consistency Centralized view ensures accuracy All users see the same data
Conclusion
Views make working with databases easier, safer, and faster. Let me know if you need
more examples! 👉
4o
1. Union (∪)
2. Intersection (∩)
3. Difference (-)
4. Cartesian Product (×)
1. Union (∪)
Combines two tables and removes duplicate rows.
Both tables must have the same columns and data types.
Example
Students_A Table
ID Name
1 Alex
2 Bob
Students_B Table
ID Name
2 Bob
3 Carol
Union Query
sql
CopyEdit
SELECT * FROM Students_A
UNION
SELECT * FROM Students_B;
Result
ID Name
1 Alex
2 Bob
3 Carol
2. Intersection (∩)
Finds common rows between two tables.
Both tables must have the same columns and data types.
Example
Intersection Query
sql
CopyEdit
SELECT * FROM Students_A
INTERSECT
SELECT * FROM Students_B;
Result
ID Name
2 Bob
3. Difference (-)
Finds rows in the first table that are NOT in the second table.
Both tables must have the same columns and data types.
Example
sql
CopyEdit
SELECT * FROM Students_A
EXCEPT
SELECT * FROM Students_B;
Result
ID Name
1 Alex
Example
Students Table
ID Name
1 Alex
ID Name
2 Bob
Courses Table
Course_ID Course_Name
101 Math
102 Science
sql
CopyEdit
SELECT * FROM Students
CROSS JOIN Courses;
Result
Summary Table
Operation Meaning Example Result
Combines two tables and removes All students from both tables
Union (∪)
duplicates (no duplicates)
Students who are in both
Intersection (∩) Finds common rows in both tables
tables
Finds rows in the first table that are NOT Students only in the first
Difference (-)
in the second table table
Cartesian Combines every row of the first table with All possible student-course
Product (×) every row of the second table combinations