0% found this document useful (0 votes)
21 views

Unit1

Uploaded by

samuofficial94
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Unit1

Uploaded by

samuofficial94
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 223

Contents

Introduction to Database Management Systems, Purpose of Database Systems,


Database-System Applications, View of Data, Database Languages, Database
System Structure, Data Models, Database Design and ER Model: Entity,
Attributes, Relationships, Constraints, Keys, Design Process, Entity Relationship
Model, ER Diagram, Design Issues, Extended E-R Features, converting E-R &
EER diagram into tables

Case Study: Design ER Model for any real time application and convert the same
into tables on paper
1.Google - Bigtable, MariaDB
2.Facebook - MariaDB, MySQL, HBase, Cassandra
3.Youtube - Bigtable, MariaDB
4.Yahoo - PostgreSQL, HBase, Cassandra, MongoDB
5.Amazon - DynamoDB, RDS/Aurora, Redshift
6.Twitter - MySQL
7.Linkedin - Venice
8.Netflix - NMDB PostgreSQL
9.MSN - Microsoft SQL Server
10.Wikipedia - MariaDB
In Gate Exam - 15-18% weightage
Introduction to Database Management Systems
Database:

The database is a collection of interrelated data which is used to retrieve, insert and delete the data

efficiently. It is also used to organize the data in the form of a table, schema, views, and reports, etc.

Using the database, you can easily retrieve, insert, and delete the information.
Database Management System :

● Database management system is a software which is used to manage the database. For example:

MySQL, Oracle, etc are a very popular commercial database which is used in different applications.

● DBMS provides an interface to perform various operations like database creation, storing data in it,

updating data, creating a table in the database and a lot more.It provides protection and security to

the database. In the case of multiple users, it also maintains data consistency.
Applications

● Banking: transactions
● Airlines: reservations, schedules
● Universities: registration, grades
● Sales: customers, products, purchases
● Online retailers: order tracking, customized recommendations
● Manufacturing: production, inventory, orders, supply chain
● Human resources: employee records, salaries, tax deductions
Purpose of Database Management System
In the early days, database applications were built directly on top of file systems

Drawbacks of using file systems to store data:

● Data redundancy and inconsistency

Multiple file formats, duplication of information in different files

● Difficulty in accessing data

Need to write a new program to carry out each new task

● Data isolation

Multiple files and formats


● Integrity problems

Integrity constraints (e.g., account balance > 0) become “buried” in program


code rather than being stated explicitly
Hard to add new constraints or change existing ones
● Concurrent access by multiple users
Concurrent access needed for performance
Uncontrolled concurrent accesses can lead to inconsistencies
Example: Two people reading a balance (say 100) and updating it by
withdrawing money (say 50 each) at the same time
● Security problems
Hard to provide user access to some, but not all, data
Database-System Applications
Railway Reservation System –

In the rail route reservation framework, the information base is needed to store the record or
information of ticket appointments, status about train’s appearance, and flight. Additionally, if trains
get late, individuals become acquainted with it through the information base update.

Social Media Sites –

We all utilization of online media sites to associate with companions and to impart our perspectives to the world.
Every day, many people group pursue these online media accounts like Pinterest, Facebook, Twitter, and Google
in addition to. By the utilization of the data set administration framework, all the data of clients are put away in the
information base and, we become ready to interface with others.
Databases are used to support internal operations of organizations and to underpin online interactions
with customers and suppliers.
Databases are used to hold administrative information and more specialized data, such as
engineering data or economic models.
Databases touch all aspects of our lives. Some of the major areas of application are as follows:
● Universities

● Banking

● Airlines

● Computerized library systems

● Flight Reservation System

● Manufacturing and selling

● Human resources

● Content Management Systems that stores websites as collections

● of WebPages in a database, etc.


View of Data
View of data in DBMS narrate how the data is visualized at each level of data
abstraction?

Data abstraction allow developers to keep complex data structures away from the users.
The developers achieve this by hiding the complex data structures through levels of
abstraction.
Links
https://www.youtube.com/watch?v=XZmGGAbHqa0

https://www.youtube.com/watch?v=0eKVizvYSUQ

https://www.youtube.com/watch?v=KG-mqHoXOXY
Data Abstraction

Data abstraction is hiding the complex data structure in order to simplify the user’s interface of
the system.

It is done because many of the users interacting with the database system are not that much computer
trained to understand the complex data structures of the database system.

To achieve data abstraction, we will discuss a Three-Schema architecture

1. Physical level (internal level)


2. Logical level (conceptual level)
3. View level (external level)
Physical Level
It is the lowest level of abstraction for DBMSs, defining how data is stored, data structures
for storing data, and database access mechanisms.

Developers or database application programmers decide how to store data in the database.
It is complex to understand.
Logical or Conceptual Level

● The logical level is the next higher level or intermediate level.

● It explains what data is stored in the database and how those data are related.

● It seeks to explain the complete or entire data by describing what tables should be

constructed and what the linkages between those tables should be.

● It is less complex than the physical level.


type instructor = record
ID : string;
name : string;
dept_name : string;
salary : integer;
end;
View or External Level

● This is the top level.


● There are various views at the view level, with each view defining only a
portion of the total data.
● It also facilitates user engagement by providing a variety of views or
numerous views of a single database.
● All users have access to the view level. This is the easiest and most simple
level.
Instances and Schemas
Similar to types and variables in programming languages

Logical Schema – the overall logical structure of the database

Example: The database consists of information about a set of customers and accounts in a bank and the

relationship between them

Physical schema– the overall physical structure of the database

Instance – the actual content of the database at a particular point in time

Physical Data Independence – the ability to modify the physical schema without changing the logical schema

Applications depend on the logical schema

In general, the interfaces between the various levels and components should be well defined so that

changes in some parts do not seriously influence others.


Database Languages
Data Definition Language (DDL)
● DDL stands for Data Definition Language. It is used to define database structure or

pattern.

● It is used to create schema, tables, indexes, constraints, etc. in the database.

● Using the DDL statements, you can create the skeleton of the database.

● Data definition language is used to store the information of metadata like the number

of tables and schemas, their names, indexes, columns in each table, constraints, etc.

create, alter, drop, truncate, rename, comment.


Data Manipulation Language (DML)

DML stands for Data Manipulation Language. It is used for accessing and manipulating data in
a database. It handles user requests.
Select, Insert, Update, Delete, Merge
Data Control Language (DCL)

DCL stands for Data Control Language. It is used to retrieve the stored or saved data.
Grant, Revoke
Transaction Control Language (TCL)

TCL is used to run the changes made by the DML statement. TCL can be grouped into a logical
transaction.
Commit, Rollback
SQL
● The most widely used commercial language

● SQL is NOT a Turing machine equivalent language

● To be able to compute complex functions SQL is usually embedded in some

higher-level language

● Application programs generally access databases through one of Language

extensions to allow embedded SQL

● Application program interface (e.g., ODBC/JDBC) which allow SQL queries to be

sent to a database
Database Applications

1.Google - Bigtable, MariaDB


2.Facebook - MariaDB, MySQL, HBase, Cassandra
3.Youtube - Bigtable, MariaDB
4.Yahoo - PostgreSQL, HBase, Cassandra, MongoDB
5.Amazon - DynamoDB, RDS/Aurora, Redshift
6.Twitter - MySQL
7.Linkedin - Venice
8.Netflix - NMDB PostgreSQL
9.MSN - Microsoft SQL Server
10.Wikipedia - MariaDB
Database Engine
● Storage manager

● Query processing

● Transaction manager
Storage Management
Storage manager is a program module that provides the interface between the
low-level data stored in the database and the application programs and
queries submitted to the system.

The storage manager is responsible to the following tasks:

Interaction with the OS file manager Efficient storing, retrieving and updating of
data
● Authorization Manager: It ensures role-based access control, i.e,. checks whether the particular
person is privileged to perform the requested operation or not.

● Integrity Manager: It checks the integrity constraints when the database is modified.

● Transaction Manager: It controls concurrent access by performing the operations in a scheduled way
that it receives the transaction. Thus, it ensures that the database remains in the consistent state before
and after the execution of a transaction.

● File Manager: It manages the file space and the data structure used to represent information in the
database.

● Buffer Manager: It is responsible for cache memory and the transfer of data between the secondary
storage and main memory.
Query Processor:

It interprets the requests (queries) received from end user via an application program into instructions. It
also executes the user request which is received from the DML compiler.
Query Processor contains the following components –
● DML Compiler: It processes the DML statements into low level instruction (machine language),
so that they can be executed.
● DDL Interpreter: It processes the DDL statements into a set of table containing meta data (data
about data).
● Embedded DML Pre-compiler: It processes DML statements embedded in an application
program into procedural calls.
● Query Optimizer: It executes the instruction generated by DML Compiler.
Transaction Management (Consistent-Durable-Recovery-Concurrent)

What if the system fails?


What if more than one user is concurrently updating the same data?
A transaction is a collection of operations that performs a single logical function in
a database application
Transaction-management component ensures that the database remains in a
consistent (correct) state despite system failures (e.g., power failures and
operating system crashes) and transaction failures.
Concurrency-control manager controls the interaction among the concurrent
transactions, to ensure the consistency of the database
Disk Storage

● Data Files: It stores the data.


● Data Dictionary: It contains the information about the structure of any database
object. It is the repository of information that governs the metadata.
● Indices: It provides faster retrieval of data item.
Data Model
Data Model is the modeling of the data description, data semantics, and consistency constraints of
the data. It provides the conceptual tools for describing the design of a database at each level of
data abstraction.

Therefore, there are following four data models used for understanding the structure of the
database:
Relational Data Model:
This type of model designs the data in the form of rows and columns within a table.

Thus, a relational model uses tables for representing data and in-between relationships.

Tables are also called relations.

This model was initially described by Edgar F. Codd, in 1969.

The relational data model is the widely used model which is primarily used by commercial data
processing applications.
Entity-Relationship Data Model:

An ER model is the logical representation of data as objects and relationships among them.
These objects are known as entities, and relationship is an association among these entities.
This model was designed by Peter Chen and published in 1976 papers.

It was widely used in database designing.

A set of attributes describe the entities.

For example, student_name, student_id describes the 'student' entity.

A set of the same type of entities is known as an 'Entity set', and the set of the same type of
relationships is known as 'relationship set'.
Example
Object-based Data Model:

An extension of the ER model with notions of functions, encapsulation, and object identity, as
well.

This model supports a rich type system that includes structured and collection types.

Thus, in 1980s, various database systems following the object-oriented approach were
developed. Here, the objects are nothing but the data carrying its properties.
Semistructured Data Model:

This type of data model is different from the other three data models

The semistructured data model allows the data specifications at places where the individual data
items of the same type may have different attributes sets.

The Extensible Markup Language, also known as XML, is widely used for representing the
semistructured data.

Although XML was initially designed for including the markup information to the text document, it
gains importance because of its application in the exchange of data.
E-R Diagram
An Entity Relationship Diagram (ER Diagram) pictorially explains the
relationship between entities to be stored in a database.

Fundamentally, the ER Diagram is a structural design of the database. It acts


as a framework created with specialized symbols for the purpose of defining
the relationship between the database entities.

ER diagram is created based on three principal components: entities,


attributes, and relationships.
IT has two entities - Student and Course, and their relationship.

The relationship described between student and course is many-to-many, as a course can be opted
by several students, and a student can opt for more than one course.

Student entity possesses attributes - Stu_Id, Stu_Name & Stu_Age.

The course entity has attributes such as Cou_ID & Cou_Name.


History of E-R Diagram
Peter Chen proposed ER Diagrams in 1971 to create a uniform convention that can be
used as a conceptual modeling tool.

Many models were presented and discussed, but none were suitable.

The data structure diagrams offered by Charles Bachman also inspired his model.
Use ER Diagrams in DBMS?

● ER Diagram helps you conceptualize the database and lets you know which fields
need to be embedded for a particular entity
● ER Diagram gives a better understanding of the information to be stored in a
database
● It reduces complexity and allows database designers to build databases quickly
● It helps to describe elements using Entity-Relationship models
● It allows users to get a preview of the logical structure of the database
Symbols Used in ER Diagrams

● Rectangles: This Entity Relationship Diagram symbol represents entity types


● Ellipses: This symbol represents attributes
● Diamonds: This symbol represents relationship types
● Lines: It links attributes to entity types and entity types with other relationship
types
● Primary key: Here, it underlines the attributes
● Double Ellipses: Represents multi-valued attributes
Components of ER Diagram
● Entities
● Weak Entity
● Attributes
● Key Attribute
● Composite Attribute
● Multivalued Attribute
● Derived Attribute
● Relationships
● One-to-One Relationships
● One-to-Many Relationships
● Many-to-One Relationships
● Many-to-Many Relationships
Entities

An entity can be either a living or non-living component.

It showcases an entity as a rectangle in an ER diagram.

For example, in a student study course, both the student and the course are entities.
Weak Entity
An entity that makes reliance over another entity is called a weak entity

You showcase the weak entity as a double rectangle in ER Diagram.

In the example below, school is a strong entity because it has a primary key attribute - school number.
Unlike school, the classroom is a weak entity because it does not have any primary key and the room
number here acts only as a discriminator.
Attribute
An attribute exhibits the properties of an entity.

You can illustrate an attribute with an oval shape in an ER diagram.


Key Attribute

Key attribute uniquely identifies an entity from an entity set.

It underlines the text of a key attribute.

For example: For a student entity, the roll number can uniquely identify a student from a set of students.
Composite Attribute

An attribute that is composed of several other attributes is known as a composite attribute.

An oval showcases the composite attribute, and the composite attribute oval is further connected with
other ovals.
Multivalued Attribute

Some attributes can possess over one value, those attributes are called multivalued attributes.

The double oval shape is used to represent a multivalued attribute.


Derived Attribute

An attribute that can be derived from other attributes of the entity is known as a derived attribute.

In the ER diagram, the dashed oval represents the derived attribute.


Relationship
The diamond shape showcases a relationship in the ER diagram.

It depicts the relationship between two entities.

In the example below, both the student and the course are entities, and study is the relationship between
them.
One-to-One Relationship

When a single element of an entity is associated with a single element of another entity, it is called a one-to-one
relationship.

For example, a student has only one identification card and an identification card is given to one person.
One-to-Many Relationship

When a single element of an entity is associated with more than one element of another entity, it is called a
one-to-many relationship

For example, a customer can place many orders, but an order cannot be placed by many customers.
Many-to-One Relationship

When more than one element of an entity is related to a single element of another entity, then it is called a
many-to-one relationship.

For example, students have to opt for a single course, but a course can have many students.
Many-to-Many Relationship

When more than one element of an entity is associated with more than one element of another entity, this
is called a many-to-many relationship.

For example, you can assign an employee to many projects and a project can have many employees.
How to Draw an ER Diagram?

Below are some important points to draw ER diagram:

● First, identify all the Entities. Embed all the entities in a rectangle and label them properly.

● Identify relationships between entities and connect them using a diamond in the middle,

illustrating the relationship. Do not connect relationships with each other.

● Connect attributes for entities and label them properly.

● Eradicate any redundant entities or relationships.

● Make sure your ER Diagram supports all the data provided to design the database.

● Effectively use colors to highlight key areas in your diagrams.


How to Create an Entity Relationship Diagram (ERD)

In a university, a Student enrolls in Courses. A student


must be assigned to at least one or more Courses. Each
course is taught by a single Professor. To maintain
instruction quality, a Professor can deliver only one
course
Step 1) Entity Identification

We have three entities


● Student
● Course
● Professor
Step 2) Relationship Identification

We have the following two relationships


● The student is assigned a course
● Professor delivers a course
Step 3) Cardinality Identification

For them problem statement we know that,


● A student can be assigned multiple courses
● A Professor can deliver only one course
Step 4) Identify Attributes

You need to study the files, forms, reports, data currently maintained by the organization to
identify attributes.

You can also conduct interviews with various stakeholders to identify entities. Initially, it’s
important to identify the attributes without mapping them to a particular entity.

Once, you have a list of Attributes, you need to map them to the identified entities. Ensure an
attribute is to be paired with exactly one entity.

If you think an attribute should belong to more than one entity, use a modifier to make it unique.

Once the mapping is done, identify the primary Keys. If a unique key is not readily available,
create one.
For Course Entity, attributes could be Duration, Credits, Assignments, etc. For the sake of ease
we have considered just one attribute.
Step 5) Create the ERD Diagram

A more modern representation of Entity Relationship Diagram Example


Best Practices for Developing Effective ER Diagrams

Here are some best practice or example for Developing Effective ER Diagrams.

● Eliminate any redundant entities or relationships


● You need to make sure that all your entities and relationships are properly labeled
● There may be various valid approaches to an ER diagram. You need to make sure that the ER
diagram supports all the data you need to store
● You should assure that each entity only appears a single time in the ER diagram
● Name every relationship, entity, and attribute are represented on your diagram
● Never connect relationships to each other
● You should use colors to highlight important portions of the ER diagram
Keys

○ Keys play an important role in the relational database.


○ It is used to uniquely identify any record or row of data from the table.
It is also used to establish and identify relationships between tables.

For example, ID is used as a key in the Student table because it is unique for each student.

In the PERSON table, passport_number, license_number, SSN are keys since they are unique
for each person.
Types of keys:
1. Primary key

○ It is the first key used to identify one and only one instance of an entity uniquely.
An entity can contain multiple keys, as we saw in the PERSON table. The key
which is most suitable from those lists becomes a primary key.
○ In the EMPLOYEE table, ID can be the primary key since it is unique for each
employee. In the EMPLOYEE table, we can even select License_Number and
Passport_Number as primary keys since they are also unique.
○ For each entity, the primary key selection is based on requirements and
developers.
2. Candidate key

○ A candidate key is an attribute or set of attributes that can uniquely identify a


tuple.
○ Except for the primary key, the remaining attributes are considered a candidate
key. The candidate keys are as strong as the primary key.

For example: In the EMPLOYEE table, id is best suited for the primary key. The rest of the
attributes, like SSN, Passport_Number, License_Number, etc., are considered a candidate
key.
Super Key

Super key is an attribute set that can uniquely identify a tuple. A super key is a superset of a
candidate key.

EMPLOYEE table, for(EMPLOEE_ID, EMPLOYEE_NAME), the name of two employees can be the same, but their EMPLYEE_ID can't be
the same. Hence, this combination can also be a key.

The super key would be EMPLOYEE-ID (EMPLOYEE_ID, EMPLOYEE-NAME), etc.


Foreign key
○ Foreign keys are the column of the table used to point to the primary key of another table.
○ Every employee works in a specific department in a company, and employee and department are
two different entities. So we can't store the department's information in the employee table. That's
why we link these two tables through the primary key of one table.
○ We add the primary key of the DEPARTMENT table, Department_Id, as a new attribute in the
EMPLOYEE table.
○ In the EMPLOYEE table, Department_Id is the foreign key, and both the tables are related.
Alternate key

There may be one or more attributes or a combination of attributes that uniquely identify each tuple in a relation.

These attributes or combinations of the attributes are called the candidate keys. One key is chosen as the primary
key from these candidate keys, and the remaining candidate key, if it exists, is termed the alternate key.

In other words, the total number of the alternate keys is the total number of candidate keys minus the primary key.
The alternate key may or may not exist.

If there is only one candidate key in a relation, it does not have an alternate key.

For example, employee relation has two attributes, Employee_Id and PAN_No, that act as candidate keys. In this
relation, Employee_Id is chosen as the primary key, so the other candidate key, PAN_No, acts as the Alternate key.
Composite key
Whenever a primary key consists of more than one attribute, it is known as a composite key.
This key is also known as Concatenated Key.

in employee relations, we assume that an employee may be assigned multiple roles, and an employee
may work on multiple projects simultaneously.
So the primary key will be composed of all three attributes, namely Emp_ID, Emp_role, and Proj_ID in
combination.
So these attributes act as a composite key since the primary key comprises more than one attribute.
Artificial key

The key created using arbitrarily assigned data are known as artificial keys. These keys are
created when a primary key is large and complex and has no relationship with many other
relations. The data values of the artificial keys are usually numbered in a serial order.

The primary key, which is composed of


Emp_ID, Emp_role, and Proj_ID, is large
in employee relations.

So it would be better to add a new


virtual attribute to identify each tuple in
the relation uniquely.
Extended E-R

The extended Entity Relationship (ER) models are three types

● Generalization
● Specialization
● Aggregation
Generalization
○ Generalization is like a bottom-up approach in which two or more entities of lower level
combine to form a higher level entity if they have some attributes in common.
○ In generalization, an entity of a higher level can also combine with the entities of the lower level
to form a further higher level entity.
○ Generalization is more like subclass and superclass system, but the only difference is the
approach. Generalization uses the bottom-up approach.
○ In generalization, entities are combined to form a more generalized entity, i.e., subclasses are
combined to make a superclass.
Faculty and Student entities can be generalized and create a higher level entity
Person.
Specialization

○ Specialization is a top-down approach, and it is opposite to Generalization. In


specialization, one higher level entity can be broken down into two lower level entities.
○ Specialization is used to identify the subset of an entity set that shares some
distinguishing characteristics.
○ Normally, the superclass is defined first, the subclass and its related attributes are
defined next, and relationship set are then added.
In an Employee management system, EMPLOYEE entity can be specialized as TESTER or
DEVELOPER based on what role they play in the company.
Aggregation
In aggregation, the relation between two entities is treated as a single entity. In
aggregation, relationship with its corresponding entities is aggregated into a higher
level entity.
Center entity offers the Course entity act as a single entity in the relationship which is
in a relationship with another entity visitor. In the real world, if a visitor visits a
coaching center then he will never enquiry about the Course only or just about the
Center instead he will ask the enquiry about both.
Reduction of ER diagram to Table
The database can be represented using the notations, and these notations can be reduced to a collection of
tables.

In the database, every entity set or relationship set can be represented in tabular form.
Rules
○ Entity type becomes a table.
a. In the given ER diagram, LECTURE, STUDENT, SUBJECT and COURSE forms individual tables.
○ All single-valued attribute becomes a column for the table.
a. In the STUDENT entity, STUDENT_NAME and STUDENT_ID form the column of STUDENT table.
Similarly, COURSE_NAME and COURSE_ID form the column of COURSE table and so on.
○ A key attribute of the entity type represented by the primary key.
a. In the given ER diagram, COURSE_ID, STUDENT_ID, SUBJECT_ID, and LECTURE_ID are the key attribute
of the entity.
○ The multivalued attribute is represented by a separate table.
a. In the student table, a hobby is a multivalued attribute. So it is not possible to represent multiple values
in a single column of STUDENT table. Hence we create a table STUD_HOBBY with column name
STUDENT_ID and HOBBY. Using both the column, we create a composite key.
○ Composite attribute represented by components.
a. In the given ER diagram, student address is a composite attribute. It contains CITY, PIN, DOOR#,
STREET, and STATE. In the STUDENT table, these attributes can merge as an individual column.
○ Derived attributes are not considered in the table.
a. In the STUDENT table, Age is the derived attribute. It can be calculated at any point of time by
calculating the difference between current date and Date of Birth.
Table Structure
Case Study
Suppose you are given the following requirements for a simple database for the National Hockey League (NHL):

the NHL has many teams,

each team has a name,

a city, a coach, a captain, and a set of players,

each player belongs to only one team,

each player has a name, a position (such as left wing or goalie), a skill level, and a set of injury records,

a team captain is also a player,

a game is played between two teams (referred to as host_team and guest_team) and has a date (such as May 11th,
1999) and a score (such as 4 to 2).

Construct a clean and concise ER diagram for the NHL database.


A university registrar’s office maintains data about the following entities:
1. courses, including number, title, credits, syllabus, and prerequisites;
2. course offerings, including course number, year, semester, section number,
instructor(s), timings, and classroom;
3. students, including student-id, name, and program;
4. instructors, including identi-cation number, name, department, and title.
Further, the enrollment of students in courses and grades awarded to students in each
course they are enrolled for must be appropriately modeled. Construct an E-R diagram
for the registrar’s office.Document all assumptions that you make about the mapping
constraints.
Company organized into DEPARTMENT. Each department has unique name and a
particular employee who manages the department. Start date for the manager is
recorded. Department may have several locations.
A department controls a number of PROJECT. Projects have a unique name, number
and a single location.
Company’s EMPLOYEE name, ssno, address, salary, sex and birth date are recorded.
An employee is assigned to one department, but may work for several projects (not
necessarily controlled by her dept). Number of hours/week an employee works on each
project is recorded; The immediate supervisor for the employee.
Employee’s DEPENDENT are tracked for health insurance purposes (dependent name,
birthdate, relationship to employee).
SQL
SQL stands for Structured Query Language and it is an ANSI (American National Standards
Institute) standard computer language for accessing and manipulating database systems.

It is used for managing data in relational database management system which stores data in the
form of tables and relationship between data is also stored in the form of tables.

SQL statements are used to retrieve and update data in a database.

SQL works with database programs like DB2, MySQL, PostgreSQL, Oracle, SQLite, SQL Server,
Sybase, MS Access and much more.

There are many different versions of the SQL language, but to be in compliance with the ANSI
standard, they support the major keyword such as SELECT, UPDATE, DELETE, INSERT, WHERE,
and others.
History of SQL

● 1970 E.F. Codd publishes Definition of Relational Model


● 1975 Initial version of SQL Implemented (D. Chamberlin)
● IBM experimental version: System R (1977) w/revised SQL
● IBM commercial versions: SQL/DS and DB2 (the early 1980s)
● Oracle introduces commercial version before IBM's SQL/DS
● INGRES 1981 & 85
● ShareBase 1982 & 86
● Data General (1984)
● Sybase (1986)
● by 1992 over 100 SQL products
Why is SQL Used?
● SQL is useful for querying and maintaining data in a relational database
management system. This is one of the most common uses of SQL.
● It is used to change or modify previous data
● It provides a large number of commands that allow us to perform a variety of
database activities.
● SQL is also used to maintain transactions in a database.
● Almost every website today is supported by a backend database. SQL is most
commonly used to connect with that database.
● Setting table, procedure, and view permissions
● To create functions, views, and to store procedures
Characteristics of SQL

Easy to learn

SQL is an extremely practical and user-friendly language. Even if you have no prior experience with technology,
you can learn the basics of the language. SQL has a syntax that is remarkably close to English, resulting in a
smooth learning curve.

Wide Variety of Commands

SQL supports a wide variety of commands such as DDL (Data Definition Language) commands like CREATE,
DROP, ALTER; DML (Data Manipulation Language) commands like INSERT, UPDATE, DELETE; DCL (Data
Control Language) commands like GRANT, REVOKE; TCL (Transaction Control Language) commands like
COMMIT, ROLLBACK, and DQL (Data Query Language) commands like SELECT.
Stored Procedures

A stored procedure is a piece of SQL code that you can save and reuse any number of times.
Stored Procedures are used to carry out one or more DML operations on a database. It’s just a
collection of SQL statements that take some input in the form of arguments, perform some work,
and may or may not return a result

Portable Language

DQL provides portability of data definitions and applications. That is, applications can be moved
from one machine to another.
Joins

SQL supports join which is a command that joins two sets of data together (i.e. two or more
tables). INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN are the joins supported by SQL.
Union

UNION command can be used to join the results of two or more SELECT statements in SQL.

Best Suited for the Client-Server Environment

SQL is well suited to a client-server environment, in which the database management


system (DBMS) is located on a server and handles client requests
High performance

In large database systems, SQL provides performance programming capabilities for highly
transactional heavy workloads.

Scalability and Flexibility

SQL is a scalable and flexible database. It’s simple to build new tables, and tables that have
already been created or those which are no longer in use can be dropped or deleted from a
database. SQL is capable of handling big data sets and several transactions.
Transactions

TCL, which supports a variety of transactions, is supported by SQL. Transactions are


logically ordered units or sequences of work that can be completed manually by a human or
automatically by a database application. SQL commands such as COMMIT, ROLLBACK,
SAVEPOINT, and others support transactions.

Security

SQL allows setting permissions in views, tables, and procedures. This ensures the safety of
data in sensitive databases. Also, constraints can be specified in SQL. These are rules that
limit the types of data that can be entered into a table column.
SQL Data Types
SQL Commands
SQL Commands - DDL - Create

CREATE TABLE table_name


(
column1 datatype,
column2 datatype,
column3 datatype,
.....
)
Example

CREATE TABLE Students


(
Roll_no INT PRIMARY KEY,
Name VARCHAR(20),
Age INT,
Address VARCHAR(30)
);
Alter

ALTER TABLE table_name ADD column_name


column-definition;

ALTER TABLE Students ADD Date_Of_Birth DATE;


Truncate

TRUNCATE TABLE table_name;


TRUNCATE TABLE Students;
Drop
DROP TABLE table_name;

DROP TABLE Students;


Select
SELECT * FROM table_name;

The SELECT query is used when we want to retrieve the data from all the
columns of the table.

SELECT column_Name_1, column_Name_2, … FROM table_name;


Example
table student_details, having ROLL_NO, FIRST_NAME, LAST_NAME and
MARKS as the fields, and some data has been stored in these fields.

Now, if we want to access all the data stored in the table, we will make use of the
SELECT command.

SELECT * FROM student_details


if we want to only show the ROLL_NO and the marks of a student.

SELECT ROLL_NO, MARKS FROM student_details


Insert
INSERT INTO table_name
(col_1, col_2,.... col_n)
VALUES (value_1, value_2, .... value_n);

INSERT INTO table_name


VALUES (value_1, value_2, .... value_n);
Example
INSERT INTO student_details (ROLL_NO, FIRST_NAME, LAST_NAME,
MARKS) VALUES (4, 'Arpit', 'Garg', 82);
INSERT INTO student_details

VALUES (5, 'Kevin', 'Mehta', 23);


Update
UPDATE table_name

SET col_1 = value_1, col_2 = value_2,...

WHERE condition;
Example
UPDATE student_details

SET MARKS = 92

WHERE ROLL_NO = 4;
UPDATE student_details

SET MARKS = 92, LAST_NAME = 'Gupta'

WHERE ROLL_NO = 4
Delete
DELETE FROM table_name;

DELETE FROM table_name WHERE condition;


Example

DELETE FROM student_details WHERE MARKS < 80;


DELETE FROM student_details;
Example
DELETE FROM student_details

SELECT * FROM student_details;


ROLLBACK; -> restore data;

SELECT * FROM student_details;


The DELETE statement is used when we want to remove some or all of the
records from the table, while the TRUNCATE statement will delete entire
rows from a table.
SQL Operator
1. Arithmetic Operators
2. Comparison Operators
3. Logical Operators
Arithmetic SQL Operators
Comparison SQL Operators
Logical SQL Operators
Select Query
Syntax:

SELECT expressions

FROM tables

WHERE conditions;
All Fields
SELECT * FROM customers;
Example
Queries
SELECT * FROM Students
WHERE stu_fees < 3500;
SELECT SUM(stu_fees), stu_class
FROM Students
GROUP BY stu_class;

GROUP BY clause is used to group the rows of the students based on the student class column.
The total fees in an individual class are summed up, and the grouped rows are displayed in the table.
SELECT * FROM Students ORDER BY stu_fees;

The ORDER BY clause is applied to the column stu_fees to sort the final result based on the fees of
the students.
SELECT * FROM Students GROUP BY stu_id HAVING
stu_age < 17;

The HAVING clause is used to fetch the records of students under 17. Also, The GROUP BY clause is
mandatory if you are using the HAVING clause in SQL.
SQL Join
Inner Join
An INNER JOIN returns rows when the join condition is satisfied in both tables.

In other words, it returns only those records that match the join condition in both
tables.

This is the most common type of SQL JOIN. It’s also the default when you don’t
specify the type of JOIN.
Example : Inner Join
color and shoes are table shown below:
The color table stores an ID number and a name for each color. The shoes table stores
an ID number for each pair of shoes, the shoe size, and an ID number that refers to a
color in the color table.

INNER JOIN joins records from the shoes table with records from the color table
through the color_id column from the shoes table. The values in this column are
the same as those in the id column of the color table, so it produces the intended
result set.
SELECT * FROM shoes

INNER JOIN color ON color.id = shoes.color_id;

The SELECT statement takes all records from the table listed after the FROM clause – in this
case, the shoes table.
Then there is an INNER JOIN with the name of the table we want to match records with (i.e. the
color table).

The ON predicate states the matching condition, which records from both tables must have.
Here the condition is that the id field from the color table and the color_id field from the
shoes table must have matching values.

If a record doesn't have a match, it will be left out of the results.


SELECT * FROM shoes
JOIN color ON color.id = shoes.color_id;

We can see the color of each pair of shoes, thanks to the INNER JOIN.
But notice that the shoes record with NULL in the color_id column is not shown: it has
not matched any of the records in the color table.
INNER JOIN on Multiple Tables
Query
SELECT * FROM shoes

JOIN color ON color.id = shoes.color_id

JOIN material ON material.id = shoes.material_id ;

only shoes with non-NULL records in the color_id and material_id columns are shown in the result set.
Outer Join
An OUTER JOIN returns all the rows from one table and some or all of the rows from another table.

There are three types of outer joins:

● LEFT OUTER JOIN returns every record in the left table and all matching records from the
right table. If there’s no match found, a NULL is shown next to the unmatched record.

● RIGHT OUTER JOIN returns every record in the right table and all matching records from the
left table. If there’s no match found, a NULL is shown next to the unmatched record.

● FULL OUTER JOIN returns all records from both tables. All unmatched records are paired
with NULLs.
The “shirt” table only has one field, “color_shirt ”

The “pants” table also has one field, “color_pants ”:

Just imagine them as two parts of an outfit: the color of your pants and the color of your
shirt. The idea is to find shirts and pants with identical colors.
The left table is the first table listed and is found after the FROM clause.

The right table is the second table listed and is found after the JOIN clause.

You can usually omit the OUTER keyword in any OUTER JOIN — FULL JOIN , LEFT
JOIN , and RIGHT JOIN will work just as well in many databases
The LEFT OUTER JOIN retrieves all records from the first (left) table and matches
them to records from the second (right) table.

Any non-matching records from the left table are also selected, but with NULL
values where the right table records would be.
SELECT color_shirt, color_pants

FROM shirt

LEFT JOIN pants ON color_shirt=color_pants;


The left table (after FROM) is “shirt” and the right table (after LEFT JOIN) is “pants”.
The ON predicate states the condition for matching records from “shirt” with records
from “pants”.

This condition is that the values in the “shirt”.“color_shirt” field and those in the
“pants”.“color_pants” field must match.

If there is no match, records from the “shirt” table will be shown, but a NULL value
is set where the matching “pants” record would be.
The matching green and blue outfits are
together.
The yellow shirt has no pants because
the “pants” table does not have any fields
with a “yellow” value.
The RIGHT OUTER JOIN works like the LEFT JOIN , but with one major difference:
it selects all records from the right table (in this case, “pants”).

The records from the left table (“shirt”) will only be shown if they match.

SELECT color_shirt, color_pants


FROM shirt
RIGHT JOIN pants ON
color_shirt=color_pants;
All the pants are shown, but there is no matching shirt for the pink pair of pants.
With LEFT JOIN, all shirts and any matching pants were returned. With
RIGHT JOIN, all pants and any matching shirts were returned. What will
happen if you use a FULL OUTER JOIN?

It shows all records from both tables. If possible, it will match the records;
if not, a NULL will be shown where the matching record would be.

SELECT color_shirt, color_pants


FROM shirt
FULL JOIN pants ON color_shirt=color_pants;
The result set contains all the records stored in the “shirt” table and in the
“pants” table.
Index and Sequence in SQL
Database Objects
Sequence
Sequence is a set of integers 1, 2, 3, … that are generated and supported by some database
systems to produce unique values on demand.
● A sequence is a user defined schema bound object that generates a sequence of numeric
values.
● Sequences are frequently used in many databases because many applications require
each row in a table to contain a unique value and sequences provides an easy way to
generate them.
● The sequence of numeric values is generated in an ascending or descending order at
defined intervals and can be configured to restart when exceeds max_value.
Syntax
CREATE SEQUENCE sequence_name

START WITH initial_value

INCREMENT BY increment_value

MINVALUE minimum value

MAXVALUE maximum value

CYCLE|NOCYCLE ;
sequence_name: Name of the sequence.
initial_value: starting value from where the sequence starts.
Initial_value should be greater than or equal
to minimum value and less than equal to maximum value.
increment_value: Value by which sequence will increment itself.
Increment_value can be positive or negative.
minimum_value: Minimum value of the sequence.
maximum_value: Maximum value of the sequence.
cycle: When sequence reaches its set_limit
it starts from beginning.
nocycle: An exception will be thrown
if sequence exceeds its max_value.
sequence query creating sequence in ascending order
Example
CREATE SEQUENCE sequence_1

start with 1

increment by 1 query will create a sequence named sequence_1.Sequence will start from 1
and will be incremented by 1 having maximum value 100. Sequence will
minvalue 0 repeat itself from start value after exceeding 100.

maxvalue 100

cycle;
sequence query creating sequence in descending order.
CREATE SEQUENCE sequence_2
start with 100
increment by -1
minvalue 1
maxvalue 100
cycle;

Above query will create a sequence named sequence_2.Sequence will start from 100 and
should be less than or equal to maximum value and will be incremented by -1 having
minimum value 1.
create a table named students with columns as id and name.
CREATE TABLE students
(
ID number(10),
NAME char(20)
);

Now insert values into table


INSERT into students VALUES(sequence_1.nextval,'Ramesh');
INSERT into students VALUES(sequence_1.nextval,'Suresh');

where sequence_1.nextval will insert id’s in id column in a sequence as defined in sequence_1.


Indexes
An index is a schema object. It is used by the server to speed up the retrieval of rows by
using a pointer.

It can reduce disk I/O(input/output) by using a rapid path access method to locate data
quickly.

An index helps to speed up select queries and where clauses, but it slows down data input,
with the update and the insert statements.

Indexes can be created or dropped with no effect on the data. In this article, we will see how
to create, delete, and uses the INDEX in the database.
If you want to reference all pages in a book that discusses a certain topic, you first refer to
the index, which lists all the topics alphabetically and is then referred to one or more specific
page numbers.

Creating Index:

CREATE INDEX index

ON TABLE column;
where the index is the name given to that index and TABLE is the name of the table on which that
index is created and column is the name of that column for which it is applied.
For multiple columns:
Syntax:
CREATE INDEX index

ON TABLE (column1, column2,.....);


Unique Indexes: Unique indexes are used for the maintenance of the integrity of the
data present in the table as well as for fast performance, it does not allow multiple
values to enter into the table.
Syntax:
CREATE UNIQUE INDEX index

ON TABLE column;
When should indexes be created:

● A column contains a wide range of values.


● A column does not contain a large number of null values.
● One or more columns are frequently used together in a where clause or a join
condition.

When should indexes be avoided:

● The table is small


● The columns are not often used as a condition in the query
● The column is updated frequently
Removing an Index: Remove an index from the data dictionary by using the DROP
INDEX command.
Syntax:
DROP INDEX index;
To drop an index, you must be the owner of the index or have the DROP ANY INDEX
privilege.
Altering an Index: To modify an existing table’s index by rebuilding, or reorganizing
the index.
ALTER INDEX IndexName

ON TableName REBUILD;
Confirming Indexes: You can check the different indexes present in a particular table given
by the user or the server itself and their uniqueness.
Syntax:
select * from USER_INDEXES;
It will show you all the indexes present in the server, in which you can locate your own tables
too.
Renaming an index: You can use the system-stored procedure sp_rename to rename any
index in the database.
Syntax:
EXEC sp_rename
index_name,
new_index_name,
N'INDEX';
Views
Views in SQL are kind of virtual tables. A view also has rows and columns as they are in a
real table in the database.

We can create a view by selecting fields from one or more tables present in the database.

A View can either have all the rows of a table or specific rows based on certain condition.
CREATING VIEWS
We can create View using CREATE VIEW statement. A View can be created from a single table or
multiple tables. Syntax:
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE condition;

view_name: Name for the View


table_name: Name of the table
condition: Condition to select rows
we will create a View named DetailsView from the table StudentDetails. Query:

CREATE VIEW DetailsView AS

SELECT NAME, ADDRESS

FROM StudentDetails

WHERE S_ID < 5;

To see the data in the View, we can query the view in the same manner as we query a
table.

SELECT * FROM DetailsView;


● we will create a view named StudentNames from the table StudentDetails.
Query:

CREATE VIEW StudentNames AS


SELECT S_ID, NAME
FROM StudentDetails
ORDER BY NAME;
● If we now query the view as,

SELECT * FROM StudentNames;


Creating View from multiple tables: In this example we will create a View named
MarksView from two tables Student Details and Student Marks. To create a View from
multiple tables we can simply include multiple tables in the SELECT statement. Query:

CREATE VIEW MarksView AS


SELECT StudentDetails.NAME, StudentDetails.ADDRESS, StudentMarks.MARKS
FROM StudentDetails, StudentMarks
WHERE StudentDetails.NAME = StudentMarks.NAME;

To display data of View MarksView:

SELECT * FROM MarksView;


Set Operations
The SQL Set operation is used to combine the two or more SQL SELECT statements.

Types of Set Operation

1. Union
2. UnionAll
3. Intersect
4. Minus
Union

○ The SQL Union operation is used to combine the result of two or more SQL SELECT queries.
○ In the union operation, all the number of datatype and columns must be same in both the
tables on which UNION operation is being applied.
○ The union operation eliminates the duplicate rows from its resultset.

SELECT column_name FROM table1

UNION

SELECT column_name FROM table2;


SELECT * FROM First

UNION

SELECT * FROM Second;


Union All
Union All operation is equal to the Union operation. It returns the set without removing duplication
and sorting the data.

Syntax:

1. SELECT column_name FROM table1


2. UNION ALL
3. SELECT column_name FROM table2;
Using the above First and Second table.

Union All query will be like:

1. SELECT * FROM First


2. UNION ALL
3. SELECT * FROM Second;
Intersect
○ It is used to combine two SELECT statements. The Intersect operation returns the common
rows from both the SELECT statements.
○ In the Intersect operation, the number of datatype and columns must be the same.
○ It has no duplicates and it arranges the data in ascending order by default.

Syntax
SELECT column_name FROM table1

INTERSECT

SELECT column_name FROM table2;


Example:

Using the above First and Second table.

Intersect query will be:

SELECT * FROM First

INTERSECT

SELECT * FROM Second;


Minus

○ It combines the result of two SELECT statements. Minus operator is used to display the rows which
are present in the first query but absent in the second query.
○ It has no duplicates and data arranged in ascending order by default.

Syntax:

SELECT column_name FROM table1

MINUS

SELECT column_name FROM table2;


Example

Using the above First and Second table.

Minus query will be:

SELECT * FROM First

MINUS

SELECT * FROM Second;


SQL Aggregate Functions

○ SQL aggregation function is used to perform the calculations on multiple rows of a


single column of a table. It returns a single value.
○ It is also used to summarize the data.
COUNT FUNCTION

○ COUNT function is used to Count the number of rows in a database table. It can work on both
numeric and non-numeric data types.
○ COUNT function uses the COUNT(*) that returns the count of all the rows in a specified table.
COUNT(*) considers duplicate and Null.

COUNT(*)

or

COUNT( [ALL|DISTINCT] expression )


Example: COUNT with WHERE
Example: COUNT()
SELECT COUNT(*)
1. SELECT COUNT(*)
2. FROM PRODUCT_MAST; FROM PRODUCT_MAST;

10 WHERE RATE>=20;

Output: 10
Output : 7
Example: COUNT() with GROUP BY
Example: COUNT() with DISTINCT
SELECT COMPANY, COUNT(*)
SELECT COUNT(DISTINCT COMPANY)
FROM PRODUCT_MAST
FROM PRODUCT_MAST;
GROUP BY COMPANY;
Output : 3
Output:

Com1 5

Com2 3

Com3 2
COUNT() with HAVING

SELECT COMPANY, COUNT(*)

FROM PRODUCT_MAST

GROUP BY COMPANY

HAVING COUNT(*)>2;

Output :

Com1 5

Com2 3
SUM Function
Sum function is used to calculate the sum of all selected columns. It works on numeric fields only.
Syntax
SUM()

or

SUM( [ALL|DISTINCT] expression )

Example: SUM()
SELECT SUM(COST)

FROM PRODUCT_MAST;
AVG function
The AVG function is used to calculate the average value of the numeric type. AVG function returns the average of
all non-Null values.

Syntax

AVG()

or

AVG( [ALL|DISTINCT] expression )

Example:

SELECT AVG(COST)

FROM PRODUCT_MAST;
MAX Function
MAX function is used to find the maximum value of a certain column. This function determines the largest value of all
selected values of a column.

Syntax

MAX()

or

MAX( [ALL|DISTINCT] expression )

Example:

SELECT MAX(RATE)

FROM PRODUCT_MAST;
MIN Function
MIN function is used to find the minimum value of a certain column. This function determines the smallest value of all
selected values of a column.

Syntax

MIN()

or

MIN( [ALL|DISTINCT] expression )

Example:

SELECT MIN(RATE)

FROM PRODUCT_MAST;
Nested Queries
A nested query in SQL contains a query inside another query. The outer query will use the
result of the inner query. For instance, a nested query can have two SELECT statements,
one on the inner query and the other on the outer query.
What are the Types of Nested Queries in SQL?
Nested queries in SQL can be classified into two different types:

● Independent Nested Queries


● Correlated Nested Queries
Independent Nested Queries

In independent nested queries, the execution order is from the innermost query to the outer query. An outer
query won't be executed until its inner query completes its execution. The outer query uses the result of the
inner query. Operators such as IN, NOT IN, ALL, and ANY are used to write independent nested queries.

● The IN operator checks if a column value in the outer query's result is present in the inner
query's result. The final result will have rows that satisfy the IN condition.
● The NOT IN operator checks if a column value in the outer query's result is not present in the
inner query's result. The final result will have rows that satisfy the NOT IN condition.
● The ALL operator compares a value of the outer query's result with all the values of the inner
query's result and returns the row if it matches all the values.
● The ANY operator compares a value of the outer query's result with all the inner query's result
values and returns the row if there is a match with any value.
Co-related Nested Queries
In co-related nested queries, the inner query uses the values from the outer query to execute the
inner query for every row processed by the outer query. The co-related nested queries run slowly
because the inner query is executed for every row of the outer query's result.
How to Write Nested Query in SQL?
We can write a nested query in SQL by nesting a SELECT statement within another SELECT
statement. The outer SELECT statement uses the result of the inner SELECT statement for
processing.

SELECT column_name [, column_name ]


FROM table1 [, table2 ]
WHERE column_name OPERATOR
(
SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE]
)

The SELECT query inside the brackets () is the inner query, and the SELECT query outside the brackets is
the outer query. The outer query uses the result of the inner query.
Examples of Nested Query in SQL
We will use the Employees and Awards table below to understand independent and co-related
nested queries. We will be using Oracle SQL syntax in our queries.

Let's create the Employees and Awards tables:

CREATE TABLE employee ( CREATE TABLE awards(


id NUMBER PRIMARY KEY, id NUMBER PRIMARY KEY,
name VARCHAR2(100) NOT NULL, employee_id NUMBER NOT NULL,
salary NUMBER NOT NULL, award_date DATE NOT NULL
role VARCHAR2(100) NOT NULL );
);
Let's add data to the tables created above:

INSERT INTO employees VALUES (1, 'Augustine Hammond', 10000, 'Developer');

INSERT INTO employees VALUES (2, 'Perice Mundford', 10000, 'Manager');

INSERT INTO employees VALUES (3, 'Cassy Delafoy', 30000, 'Developer');

INSERT INTO employees VALUES (4, 'Garwood Saffen', 40000, 'Manager');

INSERT INTO employees VALUES (5, 'Faydra Beaves', 50000, 'Developer');

INSERT INTO awards VALUES(1, 1, TO_DATE('2022-04-01', 'YYYY-MM-DD'));


INSERT INTO awards VALUES(2, 3, TO_DATE('2022-05-01', 'YYYY-MM-DD'));
Independent Nested Queries

Example 1: IN
Select all employees who won an award.

SELECT id, name FROM employees


WHERE id IN (SELECT employee_id FROM awards);
Example 2: NOT IN
Select all employees who never won an award.

SELECT id, name FROM employees


WHERE id NOT IN (SELECT employee_id) FROM awards);
Example 3: ALL
Select all Developers who earn more than all the Managers
SELECT * FROM employees

WHERE role = 'Developer'

AND salary > ALL (

SELECT salary FROM employees WHERE role = 'Manager'

);

The developer with id 5 earns (50000) more than all the managers: 2 (10000) and 4 (40000)
Example 4: ANY
Select all Developers who earn more than any Manager
SELECT * FROM employees

WHERE role = 'Developer'

AND salary > ANY (

SELECT salary FROM employees WHERE role = 'Manager'

);
The developers with id 3 and 5 earn more than any manager:

● The developer with id 3 earns (30000) more than the manager with id 2 (10000)
● The developer with id 5 earns (50000) more than the managers with id 2 (10000)
and 4 (40000)
Co-related Nested Queries
Select all employees whose salary is above the average salary of employees in their role.

SELECT * FROM employees emp1

WHERE salary > (

SELECT AVG(salary)

FROM employees emp2

WHERE emp1.role = emp2.role

);
The manager with id 4 earns more than the average salary of all managers (25000),
and the developer with id 5 earns more than the average salary of all developers
(30000). The inner query is executed for all rows fetched by the outer query. The
inner query uses the role value (emp1.role) of every outer query's row (emp1.role =
emp2.role).
We can find the average salary of managers and developers using the below query:

SELECT role, AVG(salary)


FROM employees
GROUP BY role;
Practice
UPS prides itself on having up-to-date information on the processing and
current location of each shipped item. To do this, UPS relies on a
company-wide information system. Shipped items are the heart of the UPS
product tracking information system. Shipped items can be characterized
by item number (unique), weight, dimensions, insurance amount,
destination, and final delivery date. Shipped items are received into the
UPS system at a single retail center. Retail centers are characterized by
their type, uniqueID, and address. Shipped items make their way to their
destination via one or more standard UPS transportation events (i.e.,
flights, truck deliveries). These transportation events are characterized by a
unique scheduleNumber, a type (e.g, flight, truck), and a deliveryRoute.
Please create an Entity Relationship diagram that captures this information
about the UPS system. Be certain to indicate identifiers and cardinality
constraints.
We store each employee’s name (first, last, MI), Social Security number
(SSN), street address, salary, sex (gender), and birth date. An employee is
assigned to one department, but may work on several projects, which are not
necessarily controlled by the same department. We keep track of the current
number of hours per week that an employee works on each project. We also
keep track of the direct supervisor of each employee (who is another
employee).
We want to keep track of the dependents of each employee for insurance
purposes. We keep each dependent’s first name, sex, birth date, and relationship
to the employee.

A department controls a number of projects, each of which has a unique name, a


unique number, and a single location

You might also like