Learning SQL: The Hundred Pages SQL Notes
Learning SQL: The Hundred Pages SQL Notes
Learning SQL
Theory and Pratical
Mrunal Wankhede
P age |1
INDEX
Part-1
-------------------------------------Theory Part---------------------------------------
DBMS Tutorial:
• Database…………………………………………………….……….6
▪ From Spreadsheets to Databases………………………………………,………….….6
• Types of Databases………………………………………………..9
▪ Centralized Database………………………………………………………………………..9
▪ Distributed Database………………………………………………………………………..10
▪ Relational Database………………………………………………………………………….11
▪ NoSQL Database……………………………………………………………………………….11
▪ Cloud Database…………………………………………………………………………………12
▪ Object-oriented Databases……………………………………………………………...13
▪ Hierarchical Databases……………………………………………………………………..13
▪ Network Databases…………………………………………………………………………..13
▪ Personal Database…………………………………………………………………………….13
▪ Operational Database………………………………………………………………………14
▪ Enterprise Database………………………………………………………………………….14
• What is RDBMS……………………………………………………14
▪ What is Table…………………………………………………………………………………….15
▪ What is Field……………………………………………………………………………………..16
▪ What is Row and Record………………………………………………………………….16
▪ What is Column…………………………………………………………………………………16
• DBMS Architecture………………………………………………17
▪ 1-Tier Architecture…………………………………………………………………………….18
▪ 2-Tier Architecture……………………………………………………………………………18
▪ 3-Tier Architecture……………………………………………………………………………19
• Database Language……………………………………………..24
▪ Data Definition Language (DDL)………………………………………………….25
▪ Data Manipulation Language (DML)…………………………………………..25
▪ Data Control Language (DCL)………………………………………………………25
▪ Transaction Control Language (TCL)……………………………………………26
• Join Operations………………………………………………….32
▪ Natural Join…………………………………………………………………………………….33
▪ Outer Join………………………………………………………………………………………..33
- Left Outer join………………………………………………………………………..34
- Right Outer join……………………………………………………………………..35
- Full Outer Join………………………………………………………………………..35
▪ Equi Join………………………………………………………………………………………….36
• Integrity Constraints…………………………………………..37
▪ Domain Constraints………………………………………………………………………37
▪ Entity integrity Constraints………………………………………………………….38
P age |3
• DBMS Normalization…………………………………………..40
▪ First Normal Form…………………………………………………………………41
▪ Second Normal Form……………………………………………………………42
▪ Third Normal Form……………………………………………………………….44
Transaction
• Transactions in DBMS……………………………………….46
• Transaction Property………………………………………..47
▪ Atomicity……………………………………………………………………………….48
▪ Consistency…………………………………………………………………………….49
▪ Isolation………………………………………………………………………………….49
▪ Durability………………………………………………………………………………49
• States of Transaction…………………………………………50
▪ Active State…………………………………………………………………………….50
▪ Partially Committed………………………………………………………………50
▪ Committed……………………………………………………………………………...50
▪ Failed State……………………………………………………………………………..51
▪ Aborted……………………………………………………………………………………51
• What is a Schedule……………………………………………51
▪ Serial Schedule……………………………………………………………………..52
▪ Non-Serial Schedule…………………………………………………………….53
▪ Serializable Schedule……………………………………………………………54
SQL:
Part-2
-------------------------------------SQL Tutorial-------------------------------------
• SELECT…………………………………………………………….58
• SELECT DISTINCT……………………………………………….59
• OPERATORS………………………………………………………59
• WHERE……………………………………………………………..62
• AND, OR, NOT…………………………………………………….62
• ORDER BY…………………………………………………………63
• RANDOM…………………………………………………………...63
• INSERT INTO……………………………………………………...64
• NULL VALUES…………………………………………………….65
• UPDATE…………………………………………………………….65
• DELETE…………………………………………………………….66
• TOP………………………………………………………………….66
• MIN AND MAX……………………………………………………..67
• COUNT, AVG, SUM……………………………………………….67
• LIKE…………………………………………………………………68
• WILDCARDS……………………………………………………….69
• IN…………………………………………………………………….69
• BETWEEN………………………………………………………….70
• ALIASES……………………………………………………………70
• JOINS……………………………………………………………….71
▪ Inner Join……………………………………………………………………………………….71
▪ Left Join…………………………………………………………………………………………72
▪ Right Join………………………………………………………………………………………72
▪ Full Join…………………………………………………………………………………………73
• SELF JOIN………………………………………………………….74
• UNION……………………………………………………………....74
• GROUP BY………………………………………………………….75
• HAVING……………………………………………………………..76
• EXISTS………………………………………………………………77
• ANY, ALL……………………………………………………………77
• SELECT INTO………………………………………………...……78
• INSERT INTO SELECT…………………………………………...79
• CASE……………………………………………………………...…79
• COMMENTS……………………………………………………….80
P age |5
Part-3
-------------------------------------SQL Database----------------------------------
• CREATE DB……………………………………………………….82
• DROP DB…………………………………………………………..82
• BACKUP DB………………………………………………………83
• RENAME DB……………………………………………………...84
• CREATE TABLE………………………………………………….84
• DROP TABLE……………………………………………………..85
• RENAME TABLE…………………………………………………85
• ALTER TABLE……………………………………………………85
• CONSTRAINTS…………………………………………………..86
• NOT NULL…………………………………………………………87
• UNIQUE….………………………………………………………..87
• PRIMARY KEY…………………………………………………….87
• FOREIGN KEY…………………………………………………….90
• CHECK……………………………………………………………..92
• DEFAULT………………………………………………………….93
• CREATE INDEX…………………………………………………..94
• AUTO INCREMENT………………………………………………95
• DATES……………………………………………………………...96
• VIEWS………………………………………………………………97
• SUB-QUERIES…………………………………………………….98
References
P age |6
Part - 1
-------------------------Theory------------------------
DBMS Tutorial
Database:
First, we need to understand what is Data? Number, Integers, Decimal, Float,
String, Boolean etc. all this are Data.
When you have these kind of lots of data, you need to store this data
somewhere and specially in 21 Century, because we have social media, every
second there is terabytes data uploaded on internet.
Where to store this much data?
That’s where we need Database. Database is a collection of information
organized for easy access, management and maintenance. A database is an
organized collection of data so that it can be easily accessed.
For Example: The college Database organizes the data about the admin, staff,
students and faculty etc. Using the database, you can easily retrieve, insert and
delete the information.
To manage these databases, Database Management System (DBMS) are
used. Database management system is a software which is used to manage the
database.
❖ Databases:
• Data Integrity
• Can handle massive amounts of data.
• Quickly combine different datasets.
• Automate steps for re-use.
P age |7
• Can support data for websites and applications (so most websites and
web applications or mobile applications are linked to some sort of
database to store their data and retrieve their data)
Suppose we have a laptop and we want to perform some operation but in our
laptop there is no operating system, so how can we give a command to perform
operation.
Advantages of DBMS
• Controls database redundancy: It can control data redundancy because
it stores all the data in one single database file and that recorded data is
placed in the database.
• Data sharing: In DBMS, the authorized users of an organization can share
the data among multiple users.
• Easily Maintenance: It can be easily maintainable due to the centralized
nature of the database system.
• Reduce time: It reduces development time and maintenance need.
• Backup: It provides backup and recovery subsystems which create
automatic backup of data from hardware and software failures and restores
the data if required.
• Multiple user interface: It provides different types of user interfaces like
graphical user interfaces, application program interfaces.
Disadvantages of DBMS
• Cost of hardware and software: It requires a high speed of data
processor and large memory size to run DBMS software.
• Size: It occupies a large space of disks and large memory to run them
efficiently.
• Complexity: Database system creates additional complexity and
requirements.
• Higher impact of failure: Failure is highly impacted the database because
in most of the organization, all the data stored in a single database and if
the database is damaged due to electric failure or database corruption then
the data may be lost forever.
P age |9
Types of Databases:
There are various types of databases used for storing different varieties of data:
1) Centralized Database
It is the type of database that stores data at a centralized database system. It
comforts the users to access the stored data from different locations through
several applications. These applications contain the authentication process to let
users access data securely. An example of a Centralized database can be Central
Library that carries a central database of each library in a college/university.
P a g e | 10
Advantages:
Disadvantages:
• The size of the centralized database is large, which increases the response
time for fetching the data.
• It is not easy to update such an extensive database system.
• If any server failure occurs, entire data will be lost, which could be a huge
loss.
2) Distributed Database
Unlike a centralized database system, in distributed systems, data is distributed
among different database systems of an organization. These database systems
are connected via communication links. Such links help the end-users to access
the data easily. Examples of the Distributed database are Apache Cassandra,
HBase, lgnite, etc.
• One server failure will not affect the entire data set.
3) Relational
This database is based on the relational data model, which stores data in the
form of rows(tuple) and columns(attributes), and together forms a table
(relation). A relational database uses SQL for storing, manipulating, as well as
maintaining the data. E.F. Codd invented the database in 1970. Each table in the
database carries a key that makes the data unique from others.
Examples: Relational databases are MySQL, Microsoft SQL Server, Oracle, etc.
There are following four commonly known properties of a relational model known
as ACID properties, where:
A means Atomicity: This ensures the data operation will complete either with
success or with failure. It follows the 'all or nothing' strategy. For example, a
transaction will either be committed or will abort.
C means Consistency: If we perform any operation over the data, its value
before and after the operation should be preserved. For example, the account
balance before and after the transaction should be correct. i.e., it should remain
conserved.
I means Isolation: There can be concurrent users for accessing data at the
same time from the database. Thus, isolation between the data should remain
isolated. For example, when multiple transactions occur at the same time, one
transaction effects should not be visible to the other transactions in the
database.
4) NoSQL Database
Non-SQL/Not Only SQL is a type of database that is used for storing a wide
range of data sets. It is not a relational database as it stores data not only in
tabular form but in several different ways. It came into existence when the
P a g e | 12
5) Cloud Database
A type of database where data is stored in a virtual environment and executes
over the cloud computing platform. It provides users with various cloud
computing services (SaaS, PaaS, IaaS, etc.) for accessing the database. There
are numerous cloud platforms, but the best options are:
6) Object-oriented Databases
The type of database that uses the object-based data model approach for storing
data in the database system. The data is represented and stored as objects
which are similar to the objects used in the object-oriented programming
language.
7) Hierarchical Databases
It is the type of database that stores data in the form of parent-children
relationship nodes. Here, it organizes data in a tree-like structure.
Data get stored in the form of records that are connected via links. Each child
record in the tree will contain only one parent. On the other hand, each parent
record can have multiple child records.
8) Network Databases
It is the database that typically follows the network data model. Here, the
representation of data is in the form of nodes connected via links between them.
Unlike the hierarchical database, it allows each record to have multiple children
and parent nodes to form a generalized graph structure.
9) Personal Database
Collecting and storing data on the user's system defines a Personal Database.
This database is basically designed for a single user.
The type of database which creates and updates the database in real-time. It
is basically designed for executing and handling the daily data operations in
several businesses. For example, An organization uses operational databases
for managing per day transactions.
What is RDBMS
RDBMS stands for Relational Database Management Systems.
All modern database management system like SQL, MS SQL Server, IBM DB2,
ORACLE, My-SQL and Microsoft Access are based on RDBMS.
For example,
P a g e | 15
What is Table
The RDBMS database uses tables to store data. A table is a collection of related
data entries and contains rows and columns to store data.
What is field
Every table is broken up into smaller entities called fields. Field is a smaller
entity of the table which contains specific information about every record in the
table. In the above example, the field in the student table consist of id, name,
age, course.
A row of a table is also called record. It contains the specific information of each
individual entry in the table. It is a horizontal entity in the table. For example:
The above table contains 5 records.
P a g e | 16
What is column
The main differences between DBMS and RDBMS are given below:
After observing the differences between DBMS and RDBMS, you can say that
RDBMS is an extension of DBMS. There are many software products in the
market today who are compatible for both DBMS and RDBMS. Means today a
RDBMS application is DBMS application and vice-versa.
P a g e | 17
DBMS Architecture
• The DBMS design depends upon its architecture. It helps to design, develop,
implement, and maintain the database management system. A DBMS
architecture allows dividing the database system into individual components
that can be independently modified, changed, replaced, and altered. It also
helps to understand the components of a database.
• The basic client/server architecture is used to deal with a large number of
PCs, web servers, database servers and other components that are
connected with networks.
• The client/server architecture consists of many PCs and a workstation which
are connected via the network.
• DBMS architecture depends upon how users are connected to the database to
get their request done.
• A Database stores critical information and helps access data quickly and
securely. Therefore, selecting the correct Architecture of DBMS helps in easy
and efficient data management.
1-Tier Architecture
2-Tier Architecture
3-Tier Architecture
• The 3-Tier architecture contains another layer between the client and server.
In this architecture, client can't directly communicate with the server.
• Three Tier architecture contains a presentation layer, an application layer,
and a database server. A “tier” in this case can also be referred to as a
“layer”.
• 3-Tier database Architecture design is an extension of the 2-tier client-server
architecture. A 3-tier architecture has the following layers:
• Hierarchical Model
• Network Model
• Entity-relationship Model
• Relational Model
Hierarchical Model
This database model organises data into a tree-like-structure, with a single root,
to which all the other data is linked. The heirarchy starts from the Root data,
and expands like a tree, adding child nodes to the parent nodes.
In this model, a child node will only have a single parent node.
In hierarchical model, data is organised into tree-like structure with one one-to-
many relationship between two different types of data, for example, one
department can have many courses, many professors and of course many
students.
Network Model
This was the most widely used database model, before Relational Model was
introduced.
P a g e | 22
Entity-relationship Model
E-R Models are defined to represent the relationships into pictorial form to make
it easier for different stakeholders to understand.
This model is good to design a database, which can then be turned into tables in
relational model (explained below).
Let's take an example, If we have to design a School Database, then Student will
be an entity with attributes name, age, address etc. As Address is generally
complex, it can be another entity with attributes street name, pincode, city etc,
and there will be a relationship between them.
Relational Model
This model was introduced by E.F Codd in 1970, and since then it has been the
most widely used database model. Infact, we can say the only database model
used around the world.
The basic structure of data in the relational model is tables. All the information
related to a particular type is stored in rows of that table.
A schema diagram can display only some aspects of a schema like the name of
record type, data type, and constraints. Other aspects can't be specified through
the schema diagram. For example, the given figure neither show the data type
of each data item nor the relationship among various files.
In the database, actual data changes quite frequently. For example, in the given
figure, the database changes whenever we add a new grade or add a student.
The data at a particular moment of time is called the instance of the database.
P a g e | 24
Database Language
• A DBMS has appropriate languages and interfaces to express database
queries and updates.
• Database languages can be used to read, store and update the data in the
database.
These commands are used to update the database schema that's why they come
under Data definition language.
DML stands for Data Manipulation Language. It is used for accessing and
manipulating data in a database. It handles user requests.
• DCL stands for Data Control Language. It is used to retrieve the stored or
saved data.
• The DCL execution is transactional. It also has rollback parameters. (But
in Oracle database, the execution of data control language does not have
the feature of rolling back.)
P a g e | 26
There are the following operations which have the authorization of Revoke:
TCL is used to run the changes made by the DML statement. TCL can be
grouped into a logical transaction.
In this section, we will learn and understand about the ACID properties. We will
learn what these properties stand for and what does each property is used for.
We will also understand the ACID properties with the help of some examples.
ACID Properties:
P a g e | 27
1) Atomicity:
The term atomicity defines that the data remains atomic. It means if any
operation is performed on the data, either it should be performed or executed
completely or should not be executed at all. It further means that the operation
should not break in between or execute partially. In the case of executing
operations on the transaction, the operation should be completely executed and
not partially.
Example: If Remo has account A having $30 in his account from which he
wishes to send $10 to Sheero's account, which is B. In account B, a sum of $
100 is already present. When $10 will be transferred to account B, the sum will
become $110. Now, there will be two operations that will take place. One is the
amount of $10 that Remo wants to transfer will be debited from his account A,
and the same amount will get credited to account B, i.e., into Sheero's account.
Now, what happens - the first operation of debit executes successfully, but the
credit operation, however, fails. Thus, in Remo's account A, the value becomes
$20, and to that of Sheero's account, it remains $100 as it was previously
present.
In the above diagram, it can be seen that after crediting $10, the amount is still
$100 in account B. So, it is not an atomic transaction.
The below image shows that both debit and credit operations are done
successfully. Thus the transaction is atomic.
P a g e | 28
Thus, when the amount loses atomicity, then in the bank systems, this becomes
a huge issue, and so the atomicity is the main focus in the bank systems.
2) Consistency:
The word Consistency means that the value should remain preserved always.
In DBMS the integrity of the data should be maintained, which means if a
change in the database is made, it should remain preserved always. In the case
of transactions, the integrity of the data is very essential so that the database
remains consistent before and after the transaction. The data should always be
correct.
Example:
In the above figure, there are three accounts, A, B, and C, where A is making a
transaction T one by one to both B & C. There are two operations that take
place, i.e., Debit and Credit. Account A firstly debits $50 to account B, and the
amount in account A is read $300 by B before the transaction. After the
successful transaction T, the available amount in B becomes $150. Now, A debits
$20 to account C, and that time, the value read by C is $250 (that is correct as a
debit of $50 has been successfully done to B). The debit and credit operation
from account A to C has been done successfully. We can see that the transaction
is done successfully, and the value is also read correctly. Thus, the data is
consistent. In case the value read by B and C is $300, which means that data is
inconsistent because when the debit operation executes, it will not be consistent.
P a g e | 29
3) Isolation:
4) Durability:
Therefore, the ACID property of DBMS plays a vital role in maintaining the
consistency and availability of data in the database.
Relational schema: A relational schema contains the name of the relation and
name of all columns or attributes.
Relational key: In the relational key, each row has one or more attributes. It
can identify the row in the relation uniquely.
• In the given table, NAME, ROLL_NO, PHONE_NO, ADDRESS, and AGE are
the attributes.
• The instance of schema STUDENT has 5 tuples.
• t3 = <Laxman, 33289, 8583287182, Gurugram, 20>
Properties of Relations
Join Operations:
A Join operation combines related tuples from different relations, if and only if a
given join condition is satisfied.
Example:
Employee Table
Salary Table
Result Table
P a g e | 33
1) Natural Join:
• A natural join is the set of tuples of all combinations in R and S that are
equal on their common attribute names.
Example: Let's use the above EMPLOYEE table and SALARY table:
Output:
2) Outer Join:
Example:
P a g e | 34
Employee
Fact_Workers
Output
• Left outer join contains the set of tuples of all combinations in R and S
that are equal on their common attribute names.
• In the left outer join, tuples in R have no matching tuples in S.
P a g e | 35
• Right outer join contains the set of tuples of all combinations in R and S
that are equal on their common attribute names.
• In right outer join, tuples in S have no matching tuples in R.
• Full outer join is like a left or right join except that it contains all rows
from both tables.
• In full outer join, tuples in R that have no matching tuples in S and tuples
in S that have no matching tuples in R in their common attribute name.
3) Equi Join:
Example:
CUSTOMER RELATION
PRODUCT
P a g e | 37
OUTPUT
Integrity Constraints
• Integrity constraints are a set of rules. It is used to maintain the quality of
information.
• Integrity constraints ensure that the data insertion, updating, and other
processes have to be performed in such a way that data integrity is not
affected.
• Thus, integrity constraint is used to guard against accidental damage to the
database.
1) Domain Constraints
• Domain constraints can be defined as the definition of a valid set of values
for an attribute.
• The data type of domain includes string, character, integer, time, date,
currency, etc. The value of the attribute must be available in the
corresponding domain.
Example:
P a g e | 38
• The entity integrity constraint states that primary key value can't be null.
• This is because the primary key value is used to identify individual rows in
relation and if the primary key has a null value, then we can't identify
those rows.
• A table can contain a null value other than the primary key field.
Example:
EMPLOYEE
Example:
P a g e | 39
4) Key Constraints
• Keys are the entity set that is used to identify an entity within its entity
set uniquely.
• An entity set can have multiple keys, but out of which one key will be the
primary key. A primary key can contain a unique and null value in the
relational table.
Example:
P a g e | 40
Normalization
DBMS Normalization
• Normalization is the process of organizing the data in the database.
• Normalization is used to minimize the redundancy from a relation or set of
relations. It is also used to eliminate the undesirable characteristics like
Insertion, Update and Deletion Anomalies.
• Normalization divides the larger table into the smaller table and links them
using relationship.
• The normal form is used to reduce redundancy from the database table.
EMPLOYEE Table:
P a g e | 42
In the above table in column EMP_PHONE there is multiple values in one row
that’s why it obey the 1NF.
Example: Let's assume, a school can store the data of teachers and the
subjects they teach. In a school, a teacher can teach more than one
subject.
P a g e | 43
TEACHER Table
To convert the given table into 2NF, we decompose it into two tables:
TEACHER_DETAIL table:
TEACHER_SUBJECT table:
• A relation will be in 3NF if it is in 2NF and not contain any transitive partial
dependency.
• 3NF is used to reduce the data duplication. It is also used to achieve the
data integrity.
• If there is no transitive dependency for non-prime attributes, then the
relation must be in third normal form.
A relation is in third normal form if it holds atleast one of the following conditions
for every non-trivial function dependency X → Y.
1. X is a super key.
2. Y is a prime attribute, i.e., each element of Y is part of some candidate
key.
Example:
EMPLOYEE_DETAIL table:
Non-prime attributes: In the given table, all attributes except EMP_ID are
non-prime.
That's why we need to move the EMP_CITY and EMP_STATE to the new
<EMPLOYEE_ZIP> table, with EMP_ZIP as a Primary key.
EMPLOYEE table:
P a g e | 45
EMPLOYEE_ZIP table:
P a g e | 46
Transaction
Transactions in DBMS:
• Transactions are a set of operations used to perform a logical set of work.
It contains a group of tasks.
• A transaction is an action or series of actions. It is performed by a single
user to perform operations for accessing the contents of the database.
• All types of databases access operation which are held between the
beginning and end transaction statements are considered as a single
logical transaction in DBMS. During the transaction the database is
inconsistent. Only once the database is committed the state is changed
from one consistent state to another.
• One of the major uses of DBMS is to protect the user’s data from system
failures. It is done by ensuring that all the data is restored to a consistent
state when the computer is restarted after a crash. The transaction is any
one execution of the user program in a DBMS. Executing the same
program multiple times will generate multiple transactions.
Operations of Transaction:
Read(X): Read operation is used to read the value of X from the database and
stores it in a buffer in main memory.
P a g e | 47
Write(X): Write operation is used to write the value back to the database from
the buffer.
• The first operation reads X's value from database and stores it in a buffer.
• The second operation will decrease the value of X by 500. So buffer will
contain 3500.
• The third operation will write the buffer's value to the database. So X's
final value will be 3500.
For example: If in the above transaction, the debit transaction fails after
executing operation 2 then X's value will remain 4000 in the database which is
not acceptable by the bank.
Transaction Property
The transaction has the four properties. These are used to maintain consistency
in a database, before and after the transaction.
Property of Transaction
1. Atomicity
2. Consistency
3. Isolation
4. Durability
P a g e | 48
Atomicity
Abort: If a transaction aborts then all the changes made are not visible.
Commit: If a transaction commits then all the changes made are visible.
Consistency
For example: The total amount must be maintained before or after the
transaction.
Isolation
Durability
States of Transaction
In a database, the transaction can be in one of the following states –
Active state
• The active state is the first state of every transaction. In this state, the
transaction is being executed.
• For example: Insertion or deletion or updating a record is done here. But
all the records are still not saved to the database.
Partially committed
Committed
Failed state
P a g e | 51
• If any of the checks made by the database recovery system fails, then the
transaction is said to be in the failed state.
• In the example of total mark calculation, if the database is not able to fire
a query to fetch the marks, then the transaction will fail to execute.
Aborted
• If any of the checks fail and the transaction has reached a failed state
then the database recovery system will make sure that the database is in
its previous consistent state. If not then it will abort or roll back the
transaction to bring the database into a consistent state.
• If the transaction fails in the middle of the transaction then before
executing the transaction, all the executed transactions are rolled back to
its consistent state.
• After aborting the transaction, the database recovery module will select
one of the two operations:
1. Re-start the transaction
2. Kill the transaction
What is a Schedule?
A Schedule is a process creating a single group of the multiple parallel
transactions and executing them one by one. It should preserve the order in
which the instructions appear in each transaction. If two transactions are
executed at the same time, the result of one transaction may affect the output
of other.
In a nutshell, A series of operation from one transaction to another transaction is
known as schedule
Example
1. Serial Schedule
For example: Suppose there are two transactions T1 and T2 which have some
operations. If it has no interleaving of operations, then there are the following
two possible outcomes:
1. Execute all the operations of T1 which was followed by all the operations
of T2.
2. Execute all the operations of T2 which was followed by all the operations
of T1.
• In the given (a) figure, Schedule A shows the serial schedule where T1
followed by T2.
• In the given (b) figure, Schedule B shows the serial schedule where T2
followed by T1.
P a g e | 53
2. Non-serial Schedule
3. Serializable schedule
Here,
What is Serializability?
Serializability is the process of search for a concurrent schedule who output is
equal to a serial schedule where transaction are execute one after the other.
Depending on the type of schedules, there are two types of serializability:
• Conflict
• View
P a g e | 55
▪ Create
▪ Manipulate
▪ Share/Access
SQL is the standard language for Relational Database System. All the Relational
Database Management System (RDMS) like MYSQL, MS Access, Oracle, Sybase,
Informix, Postgres and SQL Server use SQL as their standard database
language.
Applications of SQL
Data As mentioned before, SQL is one of the most widely used query language
over the databases. I’m going to list few of them here:
SQL Terms:
➢ Data
Data is defined as facts or figures, or information that’s stored in or used by a
computer.
➢ Entity
Something of interest to the database user community. Examples include
customers, parts, geographic, location, etc
➢ Column
An individual piece of data stored in a table.
➢ Row
A set of columns that together completely describe an entity or some action on
an entity. Also called a record.
➢ Table
P a g e | 57
➢ Result Set
Another name for a nonpersistent table, generally the result of an SQL query.
➢ Database
A database is organized collection of data/information so that it can be easily
accessed, managed and updated.
Data science, business intelligence, or some other facet of data analysis, you will
likely need to know SQL, along with other languages/platforms such as Python
and R. Data is everywhere, in huge quantities, and arriving at a rapid pace, and
people who can extract meaningful information from all this data are in big
demand.
P a g e | 58
Part - 2
--------------------SQL Tutorial-------------------
SQL is followed by a unique set of rules and guidelines called Syntax. This
tutorial gives you a quick start with SQL by listing all the basic SQL Syntax.
All the SQL statements start with any of the keywords like SELECT, INSERT,
UPDATE, DELETE, ALTER, DROP, CREATE, USE, SHOW.
SQL Select
The SELECT statement is used to select data from a database. It returns the
data in the form of a result table. These result tables are called result-sets.
SELECT Syntax
SELECT column1, column2, ...columnN
FROM table_name;
Here, column1, column2,… are the field names of the table you want to select
data from. If you want to select all the fields available in the table, use the
following syntax:
Inside a table, a column often contains many duplicate values; and sometimes
you only want to list the different (distinct) values.
SQL Operators
What is an Operator in SQL?
An operator is a reserved word or a character used primarily in an SQL
statement’s WHERE clause to perform operations, such as comparisons and
arithmetic operations. These Operators are used to specify conditions in an SQL
statement and to serve as conjunctions for multiple conditions in a statement.
❖ Arithmetic operators
❖ Comparison operators
❖ Logical operators
❖ Operators used to negate conditions
WHERE Syntax
SELECT column1, column2, ...columnN
FROM table_name
WHERE condition;
NOTE: The WHERE clause is not only used in SELECT statement, it is also used
in UPDATE, DELETE, etc!
The AND and OR operators are used to filter records. They are used to combine
multiple conditions to narrow data in an SQL statement. These two operators are
called as the conjunctive operators.
• The AND operator displays a record if all the conditions separated by AND
are TRUE.
• The OR operator displays a record if any of the conditions separated by
OR is TRUE.
AND Syntax
OR Syntax
NOT Syntax
SQL Order By
The ORDER BY keyword is used to sort the result-set in ascending or descending
order.
The ORDER BY keyword sorts the records in ascending order by default. To sort
the records in descending order, use the DESC keyword.
ORDER BY Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
This means that it orders by Column_1, but if some rows have the same records,
it orders them by Column_2.
There are a lot of ways to select a random record or row from a database table.
Each database server needs different SQL syntax.
MY SQL:
LIMIT 1
ORACLE:
SELECT Column_name
ORDER BY dbms_random.value)
WHERE rownum = 1
PostgreSQL:
LIMIT 1
2. If you are adding values for all the columns of the table, you do not need to
specify the column names in the SQL query, However, make sure the order of
the values is in the same order as the columns in the table. Here, the INSERT
INTO syntax would be as follows:
NOTE: A NULL value is different from a zero value or a field that contains
spaces. A field with a NULL value is one that has been left blank during record
creation!
We will have to use the IS NULL and IS NOT NULL operators instead.
IS NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
SQL Update
The SQL UPDATE Statement
The UPDATE statement is used to modify the existing records in a table. You can
use the WHERE clause with the UPDATE query to update the selected rows,
otherwise all the rows would be affected.
UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
NOTE: Be careful when updating records in a table! Notice the WHERE clause in
the UPDATE statement. The WHERE clause specifies which records that should
be updated. If you omit the WHERE clause, all records in the table will be
updated!
P a g e | 66
SQL Delete
The SQL DELETE Statement
The DELETE statement is used to delete existing records in a table.
You can use the WHERE clause with a DELETE query to delete the selected rows,
otherwise all the records would be deleted.
DELETE Syntax
DELETE FROM table_name WHERE condition;
NOTE: Be careful when deleting records in a table! Notice the WHERE clause in
the DELETE statement. The WHERE clause specifies which records should be
deleted. If you omit the WHERE clause, all records in the table will be deleted!
SQL Top
The SQL TOP Clause
The TOP clause is used to fetch a TOP N number or X percent records from a
table.
The TOP clause is useful on large tables with thousands of records. Returning a
large number of records can impact performance.
LIMIT Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
TOP number;
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
MIN() Syntax
SELECT MIN(column_name)
FROM table_name
WHERE condition;
MAX() Syntax
SELECT MAX(column_name)
FROM table_name
WHERE condition;
The COUNT() function returns the number of rows that matches a specified
criterion.
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
AVG() Syntax
SELECT AVG(column_name)
FROM table_name
WHERE condition;
SUM() Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;
P a g e | 68
SQL Like
The SQL LIKE Operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in
a column.
There are two wildcards often used in conjunction with the LIKE operator:
LIKE Syntax
Tip: You can also combine any number of conditions using AND or OR operators.
Here are some examples showing different LIKE operators with ‘%’ and ‘_’
wildcards:
SQL Wildcards
A wildcard character is used t substitute one or more characters in a string.
Wildcard characters are used with the LIKE operator. The LIKE operator is used
in a WHERE clause to search for a specified pattern in a column.
P a g e | 69
SQL In
The SQL IN operator
The IN operator allows you to specify multiple values in a WHERE clause.
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
OR:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
SQL Between
The SQL BETWEEN Operator
The BETWEEN operator selects values within a given range. The values can be
numbers, text, or dates.
The BETWEEN operator is inclusive: begin and end values are included.
P a g e | 70
BETWEEN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
SQL Aliases
SQL aliases are used to give a table, or a column in a table, a temporary name.
SELECT column_name(s)
FROM table_name AS alias_name;
SQL Join
A JOIN clause is used to combine rows from two or more tables, based on a
related column between them.
• (INNER) JOIN : Returns records that have matching values in both tables.
P a g e | 71
• LEFT (OUTER) JOIN: Returns all records from the left table, and the
matched records from the right table.
• RIGHT (OUTER) JOIN: Returns all records from the right table, and the
matched records from the left table.
• FULL (OUTER) JOIN: Returns all records when there is a match in either
left or right table.
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
P a g e | 72
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Tip: FULL OUTER JOIN and FULL JOIN are the same.
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
NOTE: FULL OUTER JOIN can potentially return very large result-sets!
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
SQL Union
The SQL UNION Operator
A UNION operator is used to combine the result-set of two or more SELECT
statements.
• Every SELECT statement within UNION must have the same number of
columns.
• The columns must also have similar data types.
• The columns in every SELECT statement must also be in the same order.
UNION Syntax
The UNION operator selects only distinct values by default. To allow duplicate
values, use UNION ALL:
NOTE: The column names in the result-set are usually equal to the column
names in the first SELECT statement.
SQL Group By
The SQL GROUP BY Statement
The GROUP BY statement groups rows that have the same values into summary
rows, like “Find the number of customers in each country”.
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Example
If you want to know the total amount of the salary on each customer, then the
GROUP BY query would be as follows.
GROUP BY NAME;
Now, let us look at a table where the CUSTOMERS table has the following
records with duplicate names-
P a g e | 76
Now again, if you want to know the total amount of salary on each customer,
then the GROUP BY query would be as follows-
SQL Having
The SQL HAVING Clause
The HAVING clause wad added to SQL because the WHERE keyword cannot be
used with aggregate functions.
HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
SQL Exists
P a g e | 77
The EXISTS operator returns TRUE if the subquery returns one or more records.
EXISTS Syntax
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
ANY means that the condition will be TRUE if the operation is true for any of the
values in the range.
ANY Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY
(SELECT column_name
FROM table_name
WHERE condition);
NOTE: The operator must be a standard comparison operator (=,<>, !=, >,
>=, <, or <=)
ALL means that the condition will be true only if the operation is true for all
values in the range.
SELECT column_name(s)
FROM table_name
WHERE column_name operator ALL
(SELECT column_name
FROM table_name
WHERE condition);
SELECT *
INTO newtable [IN externaldb]
FROM oldtable
WHERE condition;
The new table will be created with the column-names and types as defined in the
old table. You can create new column names using the AS clause.
The INSERT INTO SELECT statement copies data from one table and inserts it
into another table.
The INSERT INTO SELECT statement requires that the data types inn source and
target tables match.
Note: The existing records in the target table are unaffected.
Copy only some columns from one table into another table:
SQL Case
The SQL CASE Statement
The CASE statement goes through conditions and returns a value when the first
condition is met (Like an if-then-else statement). So, once a condition is true, it
will stop reading and return the result. If no conditions are true, it returns the
value in the ELSE clause.
CASE Syntax
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
SQL Comments
SQL Comments
P a g e | 80
Any text between –- and the end of the line will be ignored (will not be
executed).
--Select all:
SELECT * FROM Customers;
The Following example uses a single-line comment to ignore the end of a line:
Multi-line Comments
Multi-line comments start with /* and end with */.
Part - 3
P a g e | 82
--------------------SQL Database-------------------
The database developers and the users use this statement in SQL for creating
the new database in the database systems. It creates the database with the
name which has been specified in the CREATE DATABASE statement.
Syntax
The Make sure you have the admin privilege before creating any database. Once
a database is created, you can check it in the list of databases.
Following are the most important points which are required to learn while
creating a database.
This statement deletes all the views and tables if stored in the database, so be
careful while using this query in SQL.
Following are the most important points which are required to learn before
removing the database from the database system.
• This statement deletes all the data from the database. If you want to
restore the deleted data in the future, you should keep the backup of data
of that database which you want to delete.
P a g e | 83
• Another most important point is that you cannot delete that database
from the system which is currently in use by another database user. If
you do so, then drop statement shows the following error on screen.
Syntax
Syntax
Syntax
Syntax
Syntax
The column parameters specify the names of the columns of the table.
The datatype parameter specifies the type of data the column can hold (e.g.
varchar, integer, date, etc).
The new table gets the name column definitions. All columns or specific columns
can be selected.
If you create a new table using an existing table, the new table will be filled with
the existing values from the old table.
Syntax
Syntax
NOTE: Be careful before dropping a table. Deleting a table will result in loss of
complete information stored in the table!
Syntax
The RENAME TABLE and ALTER TABLE syntax help in changing the name of the
table.
Syntax
The ALTER TABLE statement is also used to add and drop various constraints on
an existing table.
P a g e | 86
SQL Constraints
SQL constraints are the rules enforced on the data columns of a table. These are
used to limit the type of data that can go into a table. This ensures the accuracy
and reliability of the data in the database.
Constraints can be column level or table level. Column level constraints apply to
a column, and table level constraints apply to the whole table.
Syntax
The NOT NULL constraint enforces a column to NOT accept NULL values.
This enforces a field to always contain a value, which means that you cannot
insert a new record, or update a record without adding a value to this field.
Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for
uniqueness for a column or set of columns.
A PRIMARY KEY constraint automatically has a UNIQUE constraint.
However, you can have many UNIQUE constraints per table, but only one
PRIMARY KEY constraint per table.
Primary key must contain UNIQUE values, and cannot contain NULL values.
If you want to create a primary key, you should define a PRIMARY KEY
constraint when you create or modify a table.
P a g e | 88
A table can have only One primary key; and in the table, this primary key can
consist of single or multiple columns (Fields).
MySQL:
NOTE: You should note that in the above example there is only one PRIMARY
KEY (pk_StudentsID). However it is made up of two columns (S_Id and
LastName)
MySQL:
The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the
PRIMARY KEY in another table. In simple words you can say that, a foreign key
in one table used to point primary key in another table.
The table with the foreign key is called the child table, and the table with the
primary key is called the referenced or parent table.
Person table
Orders table
Notice that the “PersonID” column in the “Orders” table points to the “PersonID”
column in the “Persons” table.
The “PersonID” column in the “Persons” table is the PRIMARY KEY in the
“Persons” table.
P a g e | 91
The “PersonID” column in the “Orders” table is a FOREIGN KEY in the “Orders”
table.
The FOREIGN KEY constraint prevents invalid data from being inserted into the
foreign key column, because it has to be one of the values contained in the
parent table.
MySQL:
MySQL:
• Primary key cannot be null on the other hand foreign key can be null.
• Primary key is always unique while foreign key can be duplicated.
• Primary key is uniquely identify a record in a table while foreign key is a
field in a table that is primary key in the table.
• There is only one primary key in the table on the other hand we can have
more than one foreign key in the table.
• By default primary key adds a clustered index on the other hand foreign
key does not automatically create an index, clustered or no-clustered. You
must manually create an index for foreign key.
SQL Check
SQL CHECK CONSTRAINT
The CHECK constraint is used to limit the value range that can be placed in a
column.
If you define a CHECK constraint on a column it will allow only certain values for
this column.
If you define a CHECK constraint on a table it can limit the values in certain
columns based on values in other columns in the row.
The default value will be added to all new records, if no other value is specified.
The DEFAULT constraint can also be used to insert system values, by using
functions like GETDATE():
Indexes are used to retrieve data from the database more quickly than
otherwise. The users cannot see the indexes, they are just used to speed up
searches/queries.
NOTE: Updating a table with indexes takes more time than updating a table
without (because the indexes also need an update). So, only create indexes on
columns that will be frequently searched against.
Often this is the primary key field that we would like to be created automatically
every time a new record is inserted.
To let the AUTO_INCREMENT sequence start with another value, use the
following SQL statement:
To insert a new record into the “Person” table, we will NOT have to specify a
value for the “Personid” column (a unique value will be added automatically):
The SQL statement above would insert a new record into the “Persons” table.
The “Personid” column would be assigned a unique value. The “FirstName”
column would be set to “Lars” and the “LastName” column would be set to
“Monsen”.
SQL Dates
SQL DATES
The most difficult part when working with dates is to be sure that the format of
the date you are trying to insert, matches the format of the data column in the
database.
As long as your data contain only the date portion, your queries will work as
expected. However, if a time portion is involved, it gets more complicated.
Orders Table
P a g e | 97
Now we want to select the records with an OrderDate of “2008-11-11” from the
table above.
SQL Views
SQL CREATE VIEW Statement
In SQL, a view is a virtual table based on the result-set of an SQL statement.
A view contains rows and columns, just like a real table. The fields in a view are
fields from one or more real tables in the database.
You can add SQL statements and functions to a view and present the data as if
the data were coming from one single table.
A view is created with the CREATE VIEW statement.
NOTE: A view always shows up-to-date data! The database engine recreates the
view, every time a user queries it.
A subquery is used to return data that will be used in the main query as a
condition to further restrict the data to be retrieved.
Subqueries can be used with the SELECT, INSERT, UPDATE and DELETE
statements along with the operators like =, <, >, <=, >=, IN, BETWEEN etc.
There are a few rules that subqueries must follow-
References:
1. Learning SQL book (By Alan Beaulieu)
2. W3School (Website)
3. Tutorialpoint (website)
4. Javatpoint (website)
5. Guru99 (website)
6. Geeksforgeeks (website)
7. The Complete SQL Bootcamp 2022: Go from Zero to Hero (Udemy)
8. Programming Foundations: Databases (Linkedin)
9. SQL Essential Training (Linkedin)