0% found this document useful (0 votes)
14 views12 pages

What Is A Query in DBMS: ID Name Marks

A query in a Database Management System (DBMS) is a request to manipulate data using SQL, which allows for operations like retrieving, inserting, updating, and deleting data. SQL offers advantages such as ease of use, fast data retrieval, and support for complex queries, while views provide a way to simplify access to data and enhance security. Additionally, set theoretical operations like union, intersection, difference, and Cartesian product enable combining and filtering data across multiple tables.

Uploaded by

afiyakausarbsc
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)
14 views12 pages

What Is A Query in DBMS: ID Name Marks

A query in a Database Management System (DBMS) is a request to manipulate data using SQL, which allows for operations like retrieving, inserting, updating, and deleting data. SQL offers advantages such as ease of use, fast data retrieval, and support for complex queries, while views provide a way to simplify access to data and enhance security. Additionally, set theoretical operations like union, intersection, difference, and Cartesian product enable combining and filtering data across multiple tables.

Uploaded by

afiyakausarbsc
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
You are on page 1/ 12

What is a Query in DBMS

A query in DBMS (Database Management System) is a request to retrieve, insert,


update, or delete data from a database. It is like asking a question to the database and
getting an answer.

We write queries using SQL (Structured Query Language).

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.

1. Retrieving Data (SELECT Query)

Before Execution: Students Table

ID Name Marks

1 Alex 85

2 Bob 90

3 Carol 78

Query: Get All Students' Details


SELECT * FROM Students;

Result:

ID Name Marks

1 Alex 85

2 Bob 90

3 Carol 78

👉 This query retrieves all columns and rows from the Students table.

2. Inserting Data (INSERT Query)

Query: Add a New Student (David)

INSERT INTO Students (ID, Name, Marks)


VALUES (4, 'David', 88);

After Execution: Students Table

ID Name Marks

1 Alex 85

2 Bob 90

3 Carol 78

4 David 88

👉 A new student (David) is added to the table.

3. Updating Data (UPDATE Query)

Query: Change Alex’s Marks to 95

UPDATE Students SET Marks = 95 WHERE Name = 'Alex';

After Execution: Students Table


ID Name Marks

1 Alex 95

2 Bob 90

3 Carol 78

4 David 88

👉 Alex’s marks are updated from 85 to 95.

4. Deleting Data (DELETE Query)

Query: Remove Carol from the Table

DELETE FROM Students WHERE Name = 'Carol';

After Execution: Students Table

ID Name Marks

1 Alex 95

2 Bob 90

4 David 88

👉 Carol is removed from the table.

==========================================
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

 The inner query (subquery) runs first and provides data.


 The outer query then uses that data to fetch the final result.

Example 1: Find Students with Highest Marks

Imagine we have a Students table:

ID Name Marks
1 Alex 85
2 Bob 90
3 Carol 78

Query to Find the Student with the Highest Marks

SELECT Name FROM Students


WHERE Marks = (SELECT MAX(Marks) FROM Students);

1. The inner query runs first:

SELECT MAX(Marks) FROM Students;

o It finds the highest marks (90).


2. The outer query runs next:

SELECT Name FROM Students WHERE Marks = 90;

o It finds students who have 90 marks (Bob).

Final Output

Name
Bob

Example 2: Find Employees Who Earn More Than Average Salary

Consider an Employees table:

ID Name Salary
1 Alice 3000
2 Bob 4000
3 Carol 5000

Query to Find Employees Earning More Than the Average Salary


SELECT Name FROM Employees
WHERE Salary > (SELECT AVG(Salary) FROM Employees);

How it Works

1. The inner query runs first:

SELECT AVG(Salary) FROM Employees;

oIt calculates the average salary: (3000+4000+5000)/3 = 4000.


2. The outer query runs next:

SELECT Name FROM Employees WHERE Salary > 4000;

o It finds employees earning more than 4000.

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

 To simplify complex queries.


 To provide security (users can access only selected columns).
 To reuse queries without rewriting them.

Example 1: Create a View for Top Students

Instead of writing a query every time to find students with marks above 80, we can create a
view.

Creating a View

CREATE VIEW TopStudents AS


SELECT Name, Marks FROM Students WHERE Marks > 80;

Using the View

SELECT * FROM TopStudents;

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.

Example 2: View for High-Salary Employees

Let’s create a view for employees who earn more than 4000.

Creating the View

CREATE VIEW HighSalaryEmployees AS


SELECT Name, Salary FROM Employees WHERE Salary > 4000;

Using the View

SELECT * FROM HighSalaryEmployees;

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.

1. Simplifies Complex Queries ✅

Query: Create a view for passed students (Marks ≥ 40).

sql
CopyEdit
CREATE VIEW PassedStudents AS
SELECT Name, Marks FROM Students WHERE Marks >= 40;

Now, we can retrieve passed students easily:

sql
CopyEdit
SELECT * FROM PassedStudents;

Output:

Name Marks
Name Marks

Alex 85

Bob 90

David 40

2. Enhances Data Security 🔒

Query: Create a view that hides employee salaries.

sql
CopyEdit
CREATE VIEW EmployeeNames AS
SELECT Name FROM Employees;

Output:

Name

John

Alice

👉 Employees' salaries are hidden from unauthorized users.

3. Provides Data Abstraction 🔒

A view can show only necessary columns, hiding the table structure.
For example, a CustomerView can show only customer names and phone numbers.

4. Reduces Data Duplication 🔒

A view dynamically retrieves data instead of storing duplicate records.

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 ⚡

Since views store pre-defined queries, they execute faster.

7. Supports Logical Independence 🔒

If we rename a column in the main table, the view can be updated without affecting users.

8. Simplifies Report Generation 🔒

A monthly sales report view can be created once and used repeatedly.

9. Allows Virtual Table Creation 🔒

A view does not take extra space; it just presents data dynamically.

10. Enhances Data Consistency ✅

Ensures all users see the same, updated data without modifying the actual table.

Final Summary Table

Advantage Query Example Output

Simplifies Complex Queries SELECT * FROM PassedStudents; List of passed students

Only employee names, no


Enhances Data Security SELECT * FROM EmployeeNames;
salaries

SELECT Name, Phone FROM Only necessary customer


Provides Data Abstraction CustomerView;
info

Reduces Data Duplication View dynamically retrieves data No duplicate records

Improves Maintainability Table changes don’t affect views Views still work
Advantage Query Example Output

Enables Faster Query


Pre-defined queries execute faster Quick results
Execution

Supports Logical
View works even after table changes No impact on users
Independence

Simplifies Report
SELECT * FROM MonthlySales; Predefined sales report
Generation

Allows Virtual Table


View does not store data Saves storage space
Creation

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

Set Theoretical Operations on Relations in DBMS (Simple Explanation with


Easy Examples)

In DBMS (Database Management System), we can perform operations on tables (relations)


just like we do with mathematical sets. These operations help us combine or filter data from
multiple tables.

The main Set Theoretical Operations in DBMS are:

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

We have two tables: Students_A and Students_B.

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

👉 Duplicates are removed automatically.

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

👉 Only "Bob" is common in both tables, so it appears in the result.

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

Difference Query (Students_A - Students_B)

sql
CopyEdit
SELECT * FROM Students_A
EXCEPT
SELECT * FROM Students_B;

Result

ID Name
1 Alex

👉 Only "Alex" is in Students_A but not in Students_B.

4. Cartesian Product (×)


 Combines every row from the first table with every row from the second table.
 The result contains all possible combinations.

Example

We have two tables: Students and Courses.

Students Table

ID Name
1 Alex
ID Name
2 Bob

Courses Table

Course_ID Course_Name
101 Math
102 Science

Cartesian Product Query

sql
CopyEdit
SELECT * FROM Students
CROSS JOIN Courses;

Result

ID Name Course_ID Course_Name


1 Alex 101 Math
1 Alex 102 Science
2 Bob 101 Math
2 Bob 102 Science

👉 Each student is paired with each course.

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

You might also like