0% found this document useful (0 votes)
18 views11 pages

Define Subquery With The Help of Example?

Uploaded by

atmakuritagore22
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)
18 views11 pages

Define Subquery With The Help of Example?

Uploaded by

atmakuritagore22
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/ 11

Define subquery with the help of example?

A subquery is a query nested inside another query. It is used to retrieve data


that depends on the results of another query.
SELECT *
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

For the following schema write the queries using aggregate operators?
Sailors(sid,sname,rating ,age);
a) Find the average age of sailors.
b) Find the name and age of the oldest sailor?
SELECT AVG(age) AS average_age
FROM Sailors;
SELECT sname, age
FROM Sailors
ORDER BY age DESC
LIMIT 1;

How we can we retrieve single row using relational operators and multiple rows
using nested query operators?

You can use nested query operators such as IN , EXISTS , NOT


EXISTS , ALL , ANY to retrieve multiple rows from a table. For example
SELECT *
FROM Sailors
WHERE rating IN (SELECT rating FROM Sailors WHERE age > 25);

Demonstrate distinct keyword with the help of example?


The DISTINCT keyword is used to retrieve only unique values from a column or
set of columns. It eliminates duplicate values from the result set.
Example:
Suppose we have a table Sailors with the following data:
sid sname rating age
1 John 5 25
2 Mary 5 30

Write sql queries for the following schema?


Employee(eid, ename, bdate, address ,salary, dno)
a) Find the name and address of the employees with salary greater
than 10,000 and less than 60,000 using between keyword.
b)Find the name and salary of the employee whose name starts with J ,
third character is h and ends with n.

SELECT ename, address


FROM Employee
WHERE salary BETWEEN 10000 AND 60000;

SELECT ename, salary


FROM Employee
WHERE ename LIKE 'J_h%n';

SAQS

Explain about the drawbacks of file system and advantages of dbms?

Drawbacks of File System:


. Data Redundancy: Data is duplicated in multiple files, leading to
inconsistencies and data integrity issues.
. Data Inconsistency: Data in different files may not be consistent,
leading to errors and inaccuracies.
. Data Security: Files can be easily accessed, modified, or deleted by
unauthorized users, compromising data security.
. Data Sharing: Files are not easily shareable among multiple users or
applications, leading to data silos.
. Data Retrieval: Retrieving specific data from a file system can be
time-consuming and inefficient.
. Scalability: File systems can become cumbersome and difficult to
manage as the volume of data grows.

Advantages of DBMS:
. Data Integrity: DBMS enforces data integrity constraints, ensuring
data consistency and accuracy.
. Data Security: DBMS provides robust security features, such as
access control and encryption, to protect data from unauthorized
access.
. Data Sharing: DBMS allows multiple users and applications to share
data, promoting collaboration and data reuse.
. Data Retrieval: DBMS provides efficient data retrieval mechanisms,
such as indexing and querying, to quickly access specific data.
. Scalability: DBMS can handle large volumes of data and scale to meet
growing data needs.
. Better Decision Making: DBMS provides a single, unified view of the
data, enabling better decision making and business intelligence.
Compare and contrast various data models?

Comparison of Various Data Models:


There are several data models used in database design, each with its own
strengths and weaknesses. Here's a comparison of some of the most common
data models:
. Hierarchical Data Model:
○ Uses a tree-like structure to organize data.
○ Each record has a single parent-child relationship.

. Network Data Model:


○ Uses a graph-like structure to organize data.
○ Each record can have multiple parent-child relationships.

. Relational Data Model:


○ Uses tables to organize data, with each table having rows and

columns.
○ Each row represents a single record, and each column represents a

field.
. Object-Oriented Data Model:
○ Uses objects to represent data, with each object having properties

and methods.
. Entity-Relationship Data Model:
○ Uses entities and relationships to represent data.
○ Entities are objects or concepts, and relationships describe how

they interact.
Comparison Table:
Data Model Structure Flexibility Scalability Complexity
Hierarchical Tree-like Low Low Low
Network Graph-like Medium Medium Medium
Relational Tabular High High Medium
Object- Object- High High High
Oriented based
Entity- Entity-based High High High
Relationship
Contrasting Points:
. Structure: Hierarchical and network data models use a more rigid
structure, while relational, object-oriented, and entity-relationship data
models use a more flexible structure.
. Flexibility: Relational, object-oriented, and entity-relationship data
models are more flexible than hierarchical and network data models.
. Scalability: Relational, object-oriented, and entity-relationship data
models are more scalable than hierarchical and network data models.
Explain about the following? a)Weak entities illustrate with the help of example?
b)Aggregation demonstrate with the help of suitable example?

a) Weak Entities:
A weak entity is an entity that cannot exist independently and relies on another
entity for its existence. It is also known as a dependent entity. A weak entity is
represented by a rectangle with a double border.
Example:
Consider a university database that stores information about students, courses,
and grades. In this database, a grade is a weak entity because it cannot exist
independently without a student and a course.
● Student (strong entity)
○ Student ID (primary key)
○ Name
○ Address
● Course (strong entity)
○ Course ID (primary key)
○ Course Name
○ Credits
● Grade (weak entity)
○ Grade ID (primary key)
○ Student ID (foreign key referencing Student)
○ Course ID (foreign key referencing Course)
○ Grade

In this example, a grade is a weak entity because it relies on both a student and
a course for its existence. A grade cannot exist without a student and a course.

b) Aggregation:
Aggregation is a process of combining two or more entities to form a new
entity. It is a way of representing a whole-part relationship between entities.
Example:
Consider a university database that stores information about departments,
professors, and courses. In this database, a department is an aggregate of
professors and courses.
● Department (aggregate entity)
○ Department ID (primary key)
○ Department Name
○ Professors (aggregated entity)
◆ Professor ID (primary key)
◆ Professor Name
○ Courses (aggregated entity)
◆ Course ID (primary key)
◆ Course Name

In this example, a department is an aggregate of professors and courses. A


department is composed of multiple professors and courses, and it cannot exist
without them.

What is conceptual design with the ER model? Explain about the following with
suitable examples? a)Entity versus attribute. Binary versus Ternary relationship.

Conceptual Design with the ER Model:


Conceptual design is the process of creating a conceptual representation of a
database using the Entity-Relationship (ER) model.
a) Entity versus Attribute:
In the ER model, an entity is a thing or concept that has independent existence,
whereas an attribute is a characteristic or property of an entity.
Example:
Consider a university database that stores information about students. In this
database, a student is an entity, and the student's name, address, and date of
birth are attributes of the student entity.
● Entity: Student
○ Attributes:
◆ Student ID (primary key)
◆ Name
◆ Address
◆ Date of Birth

In this example, the student is an entity, and the student's name, address, and
date of birth are attributes of the student entity.
Entity:
● A person, place, object, event, or concept that has independent
existence.
● Represented by a rectangle in an ER diagram.
Attribute:
● A characteristic or property of an entity.
● Represented by a column in an ER diagram.
b) Binary versus Ternary Relationship:
In the ER model, a relationship is a connection between two or more entities. A
binary relationship is a relationship between two entities, whereas a ternary
relationship is a relationship between three entities.
Binary Relationship:
● A relationship between two entities.
● Represented by a line connecting two entities in an ER diagram.
Example:
Consider a university database that stores information about students and
courses. In this database, a student is enrolled in a course, and this is
represented by a binary relationship between the student and course entities.
● Entity: Student
○ Attributes:
◆ Student ID (primary key)
◆ Name
◆ Address
◆ Date of Birth
● Entity: Course
○ Attributes:
◆ Course ID (primary key)
◆ Course Name
◆ Credits
● Relationship: Enrolled In (binary relationship between Student and
Course)
In this example, the enrolled in relationship is a binary relationship between the
student and course entities.
Ternary Relationship:
● A relationship between three entities.
● Represented by a line connecting three entities in an ER diagram.
Example:
Consider a university database that stores information about students, courses,
and grades. In this database, a student is enrolled in a course and receives a
grade, and this is represented by a ternary relationship between the student,
course, and grade entities.
● Entity: Student
○ Attributes:
◆ Student ID (primary key)
◆ Name
◆ Address
◆ Date of Birth
● Entity: Course
○ Attributes:
◆ Course ID (primary key)
◆ Course Name
◆ Credits
● Entity: Grade
○ Attributes:
◆ Grade ID (primary key)
◆ Grade
● Relationship: Enrolled In and Receives (ternary relationship between
Student, Course, and Grade)
In this example, the enrolled in and receives relationship is a ternary
relationship between the student, course, and grade entities.

Mod 4
What is integrity constraint? Explain about the following constraints with the
help of examples? a)Primary key b)Candidate key c)Super key Foreign key?

An integrity constraint is a rule that ensures the accuracy and consistency of


data in a database.
Constraints:
. Primary Key (PK): Uniquely identifies each row in a table.
○ Example: Employee ID in an Employees table.

. Candidate Key (CK): Uniquely identifies each row, but not the primary
key.
○ Example: Social Security Number in an Employees table.

. Super Key (SK): Uniquely identifies each row, but not the primary key.
○ Example: Combination of Employee ID and Social Security Number.

. Foreign Key (FK): Establishes a relationship between two tables.


○ Example: Customer ID in an Orders table referencing the Customer

ID in a Customers table.

CREATE TABLE Employees (


EmployeeID INT PRIMARY KEY,
Name VARCHAR(255),
Address VARCHAR(255),
PhoneNumber VARCHAR(20)
);

CREATE TABLE Employees (


EmployeeID INT,
SocialSecurityNumber VARCHAR(20),
Name VARCHAR(255),
Address VARCHAR(255),
PhoneNumber VARCHAR(20),
UNIQUE (EmployeeID),
UNIQUE (SocialSecurityNumber)
);

CREATE TABLE Employees (


EmployeeID INT PRIMARY KEY,
SocialSecurityNumber VARCHAR(20),
Name VARCHAR(255),
Address VARCHAR(255),
PhoneNumber VARCHAR(20),
UNIQUE (EmployeeID, SocialSecurityNumber)
);

CREATE TABLE Customers (


CustomerID INT PRIMARY KEY,
Name VARCHAR(255),
Address VARCHAR(255),
PhoneNumber VARCHAR(20)
);

CREATE TABLE Orders (


OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

Explain about the following commands with examples?


a) Alter command
b) Update command
c)Delete command

a) Alter Command:
The ALTER command is used to modify the structure of a database table. It can
be used to add, delete, or modify columns, indexes, or constraints.

CREATE TABLE Employees (


EmployeeID INT PRIMARY KEY,
Name VARCHAR(255),
Address VARCHAR(255),
PhoneNumber VARCHAR(20)
);

To add a new column called "Email" to the table, we can use the following
ALTER command:
ALTER TABLE Employees
ADD Email VARCHAR(255);

b) Update Command:
The UPDATE command is used to modify the data in a database table. It can be
used to update one or more columns in a table.
To update the "PhoneNumber" column for a specific employee, we can use the
following UPDATE command:
UPDATE Employees
SET PhoneNumber = '123-456-7890'
WHERE EmployeeID = 1;

c) Delete Command:
The DELETE command is used to delete data from a database table. It can be
used to delete one or more rows from a table.
To delete all employees from the table, we can use the following DELETE
command:
DELETE FROM Employees
WHERE EmployeeID > 0;
Define relational algebra ? Explain about the following operators with suitable
examples? a)Selection operator b) projection operator Renaming operator

Relational Algebra:
Relational algebra is a formal system for manipulating relational databases.
Relational algebra provides a way to specify queries and operations on
relational databases in a formal and unambiguous way.
Relational Algebra Operators:
Relational algebra provides a set of operators that can be used to manipulate
relational databases. These operators can be combined to form more complex
queries and operations.
a) Selection Operator (σ):
The selection operator (σ) is used to select a subset of rows from a relation
based on a condition.
Example:
Suppose we have a relation called "Employees" with the following columns:
EmployeeID Name Age Department
1 John 25 Sales
2 Jane 30 Marketing
3 Joe 35 Sales
4 Sarah 20 Marketing
To select all employees who are in the Sales department, we can use the
following selection operator:
σ Department = 'Sales' (Employees)
The result of this operation would be:
EmployeeID Name Age Department
1 John 25 Sales
3 Joe 35 Sales
b) Projection Operator (π):
The projection operator (π) is used to select a subset of columns from a
relation.
Example:
Suppose we have a relation called "Employees" with the following columns:
EmployeeID Name Age Department
1 John 25 Sales
2 Jane 30 Marketing
3 Joe 35 Sales
4 Sarah 20 Marketing
To select only the EmployeeID and Name columns from the Employees relation,
we can use the following projection operator:
π EmployeeID, Name (Employees)
The result of this operation would be:
EmployeeID Name
1 John
2 Jane
3 Joe
EmployeeID Name
1 John
2 Jane
3 Joe
4 Sarah
c) Renaming Operator (ρ):
The renaming operator (ρ) is used to rename one or more columns in a relation.
Example:
Suppose we have a relation called "Employees" with the following columns:
EmployeeID Name Age Department
1 John 25 Sales
2 Jane 30 Marketing
3 Joe 35 Sales
4 Sarah 20 Marketing
To rename the Department column to "Dept", we can use the following
renaming operator:
ρ Dept/Department (Employees)
The result of this operation would be:
EmployeeID Name Age Dept
1 John 25 Sales
2 Jane 30 Marketing
3 Joe 35 Sales
4 Sarah 20 Marketing

Discuss about views in detail? Write the following sql queries to create
views for the following schema?
Employee (eid , ename , address , salary , deptid) Dependent( eid ,
dependent_name , sex , bdate , relationship)
a)Create a view that contains all the records of the employee table?
b)Create a view that contains name of all employee and his/her
dependent? Create a view that contains details of all the female
dependents?

Views:
A view is a virtual table based on the result of a SQL query. It is a way to
simplify complex queries and provide a layer of abstraction between the
physical tables and the users. Views can be used to:
● Simplify complex queries
● Provide a layer of abstraction between the physical tables and the
users
● Restrict access to certain columns or rows of a table
● Create a virtual table that combines data from multiple tables
SQL Queries to Create Views:
Here are the SQL queries to create views for the given schema:
a) Create a view that contains all the records of the employee table:
CREATE VIEW AllEmployees AS
SELECT *
FROM Employee;

b) Create a view that contains name of all employee and his/her


dependent:
CREATE VIEW EmployeeDependent AS
SELECT E.ename, D.dependent_name
FROM Employee E
JOIN Dependent D ON E.eid = D.eid;

c) Create a view that contains details of all the female dependents:


CREATE VIEW FemaleDependents AS
SELECT *
FROM Dependent
WHERE sex = 'F';

MOD-3

You might also like