sql_tutorial
sql_tutorial
SQL Examples
Consider we have following CUSTOMERS table which stores customer's ID, Name, Age,
Salary, City and Country −
SQL makes it easy to manipulate this data using simple DML (Data Manipulation Language)
Statements. For example, if we want to list down all the customers from USA then following
will be the SQL query.
i
SQL Tutorial
So now, you do not need to do a sophisticated setup to execute all the examples given in
this tutorial because we are providing you Online SQL Editor, which allows you to edit
your code and compile it online. You can try our Online SQL Editor.
ii
SQL Tutorial
A Data Manipulation Language (DML) is used for adding, deleting, and modifying data in
a database.
iii
SQL Tutorial
SQL Applications
SQL is one of the most widely used Query Language over the databases. SQL provides
following functionality to the database programmers −
Executes different database queries against a database.
Defines the data in a database and manipulates that data.
Creates data in a relational database management system.
Accesses data from the relational database management system.
Creates and drops databases and tables.
Creates and maintains database users.
Creates views, stored procedures, functions in a database.
Sets permissions on tables, procedures and views.
iv
SQL Tutorial
v
SQL Tutorial
vi
SQL Tutorial
You can use our simple and the best SQL tutorial to learn SQL. We have removed all the
unnecessary complexity while teaching you SQL concepts. You can start learning it
now Start Learning SQL.
vii
SQL Tutorial
Table of Contents
About the Tutorial ............................................................................................................................................ i
1. SQL - Roadmap.............................................................................................................................................. 14
2. SQL - Overview.............................................................................................................................................. 16
7. SQL - Operators............................................................................................................................................. 54
9. SQL - Comments............................................................................................................................................ 63
SQL DATABASE.........................................................................................................65
SQL TABLE...............................................................................................................85
viii
SQL Tutorial
ix
SQL Tutorial
x
SQL Tutorial
xi
SQL Tutorial
xii
SQL Tutorial
SQL Tutorial
xiii
1. SQL - Roadmap SQL Tutorial
This Roadmap will guide you to master SQL. You will learn core concepts, techniques, and
best practices by following mentioned topics. This step-by-step path will help you as a
beginner.
SQL Roadmap
This SQL roadmap is created to upgrade you from a SQL novice to a SQL expert. Whether
you're aspiring to become a data analyst, database administrator, or software developer,
this comprehensive guide will provide a structured path to master SQL.
14
2. SQL - Overview SQL Tutorial
What is SQL?
SQL (Structured Query Language) is a language to operate databases. It includes
Database Creation, Database Deletion, Fetching Data Rows, Modifying & Deleting Data
rows, etc.
SQL stands for Structured Query Language which is a computer language for storing,
manipulating and retrieving data stored in a relational database. SQL was developed in
the 1970s by IBM Computer Scientists and became a standard of the American National
Standards Institute (ANSI) in 1986, and the International Organization for Standardization
(ISO) in 1987.
SQL is the standard language to communicate with Relational Database Systems. All the
Relational Database Management Systems (RDMS) like MySQL, MS Access, Oracle,
Sybase, Informix, Postgres and SQL Server use SQL as their Standard Database Language.
Why SQL?
SQL is widely popular because it offers the following advantages −
Allows users to define the data in a database and manipulate that data.
Allows to embed within other languages using SQL modules, libraries & pre-
compilers.
1978 − IBM worked to develop Codd's ideas and released a product named
System/R.
15
SQL Tutorial
There are various components included in this process. These components are −
Query Dispatcher
Optimization Engines
A classic query engine handles all the non-SQL queries, but a SQL query engine won't
handle logical files. Following is a simple diagram showing the SQL Architecture −
16
3. SQL - RDBMS Concepts SQL Tutorial
What is RDBMS?
RDBMS stands for Relational Database Management System. RDBMS is the basis for SQL,
and for all modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, and
Microsoft Access.
What is a Table?
The data in an RDBMS is stored in database objects known as tables. This table is basically
a collection of related data entries and it consists of numerous columns and rows.
Remember, a table is the most common and simplest form of data storage in a relational
database. Following is an example of a CUSTOMERS table which stores customer's ID,
Name, Age, Salary, City and Country −
17
SQL Tutorial
What is a Field?
Every table is broken up into smaller entities called fields. A field is a column in a table
that is designed to maintain specific information about every record in the table.
For example, our CUSTOMERS table consists of different fields like ID, Name, Age, Salary,
City and Country.
What is a Column?
A column is a vertical entity in a table that contains all information associated with a
specific field in a table.
For example, our CUSTOMERS table have different columns to represent ID, Name, Age,
Salary, City and Country.
It is very important to understand that a NULL value is different than a zero value or a
field that contains spaces. A field with a NULL value is the one that has been left blank
during a record creation. Following table has three records where first record has NULL
value for the salary and second record has a zero value for the salary.
18
SQL Tutorial
SQL Constraints
Constraints are the rules enforced on data columns on 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 either be column level or table level. Column level constraints are applied
only to one column whereas, table level constraints are applied to the entire table.
Following are some of the most commonly used constraints available in SQL −
S.No. Constraints
DEFAULT Constraint
2
Provides a default value for a column when none is specified.
UNIQUE Key
3
Ensures that all the values in a column are different.
PRIMARY Key
4
Uniquely identifies each row/record in a database table.
FOREIGN Key
5
Uniquely identifies a row/record in any other database table.
CHECK Constraint
6
Ensures that all values in a column satisfy certain conditions.
19
SQL Tutorial
INDEX Constraint
7
Used to create and retrieve data from the database very quickly.
Data Integrity
The following categories of data integrity exist with each RDBMS −
Entity Integrity − This ensures that there are no duplicate rows in a table.
Domain Integrity − Enforces valid entries for a given column by restricting the
type, the format, or the range of values.
Referential integrity − Rows cannot be deleted, which are used by other records.
User-Defined Integrity − Enforces some specific business rules that do not fall
into entity, domain or referential integrity.
Database Normalization
Database normalization is the process of efficiently organizing data in a database. There
are two reasons of this normalization process −
Eliminating redundant data, for example, storing the same data in more than one
table.
Both these reasons are worthy goals as they reduce the amount of space a database
consumes and ensures that data is logically stored. Normalization consists of a series of
guidelines that help guide you in creating a good database structure.
Normalization guidelines are divided into normal forms; think of a form as the format or
the way a database structure is laid out. The aim of normal forms is to organize the
database structure, so that it complies with the rules of first normal form, then second
normal form and finally the third normal form.
It is your choice to take it further and go to the Fourth Normal Form, Fifth Normal Form
and so on, but in general, the Third Normal Form is more than enough for a normal
Database Application.
20
4. SQL Databases SQL Tutorial
SQL Databases
SQL or Relational databases are used to store and manage the data objects that are related
to one another, i.e. the process of handling data in a relational database is done based on
a relational model.
This relational model is an approach to manage data in a structured way (using tables). A
system used to manage these relational databases is known as Relational Database
Management System (RDBMS).
Columns: Columns are vertical elements in a table. Each column in a table holds specific
attribute information, and column properties such as column name and data types
(Integer, Char, String, etc.).
Rows: Rows are horizontal elements in a table and users can add data or retrieve by
executing SQL queries.
MySQL
MS SQL Server
ORACLE
MS ACCESS
PostgreSQL
SQLite
This SQL databases tutorial gives a brief overview of these RDBMS specified above. This
would help you to compare their basic features.
MySQL
MySQL is an open source SQL database, which is developed by a Swedish company, MySQL
AB. MySQL is pronounced as "my ess-que-ell," in contrast with SQL, pronounced "sequel."
MySQL is supporting different platforms including Microsoft Windows, the major Linux
distributions, UNIX, and Mac OS X.
21
SQL Tutorial
MySQL has free and paid versions, depending on its usage (non-commercial/commercial)
and features. MySQL comes with a very fast, multi-threaded, multi-user and robust SQL
database server.
History
1998 - Windows Version was released on the 8 th January 1998 for Windows 95 and
NT.
2001 - Version 3.23 released beta from June 2000, and production released in
January 2001.
2003 - Version 4.0 was released on August 2002 as beta, and as production
released in March 2003 (unions).
2004 - Version 4.1 was released on June 2004 as beta, and as production released
in October 2004.
2005 - Version 5.0 was released on March 2005 as beta, and as a production
released in October 2005.
2018 - General availability of Version 8.0 was enabled on 19th April 2018, and is
the latest version of MySQL.
Features
High Performance.
High Availability.
Management Ease.
22
SQL Tutorial
MS SQL Server
MS SQL Server is a Relational Database Management System developed by Microsoft Inc.
Its primary query languages are −
T-SQL
ANSI SQL
History
1989 - Microsoft, Sybase, and Aston-Tate released SQL Server 1.0 for OS/2.
1990 - SQL Server 1.1 is released with support for Windows 3.0 clients.
2001 - Microsoft released XML for SQL Server Web Release 1 (download).
2002 - Microsoft released SQLXML 2.0 (renamed from XML for SQL Server).
2008 - Microsoft released SQL Server 2008 on November 6, 2008 and R2 version
was released on July 20, 2010.
Features
High Performance
High Availability
Database mirroring
Database snapshots
CLR integration
Service Broker
DDL triggers
Ranking functions
23
SQL Tutorial
XML integration
TRY...CATCH
Database Mail
ORACLE
Oracle is a very large multi-user based database management system developed by
'Oracle Corporation'.
Oracle works to efficiently manage its resources, a database of information among the
multiple clients requesting and sending data in the network.
It is an excellent database server choice for client/server computing. Oracle supports all
major operating systems for both clients and servers, including MSDOS, NetWare,
UnixWare, OS/2 and most UNIX flavors.
History
Oracle began in 1977 and celebrating its 46 wonderful years in the industry (from 1977 to
2023).
1977 - Larry Ellison, Bob Miner and Ed Oates founded Software Development
Laboratories to undertake development work.
1979 - Version 2.0 of Oracle was released and it became first commercial relational
database and first SQL database. The company changed its name to Relational
Software Inc. (RSI).
1983 - Oracle released version 3.0, rewritten in C language and ran on multiple
platforms.
1984 - Oracle version 4.0 was released. It contained features like concurrency
control - multi-version read consistency, etc.
2007 - Oracle released Oracle11g. The new version focused on better partitioning,
easy migration, etc.
2013 - Oracle Database 12c R1 version was released in July, 2013; and R2 version
was released on cloud in August 2016, and released on-premise in March 2017.
2018 - Oracle Database 18c version was initially released in July 2018.
Features
Concurrency
Read Consistency
Locking Mechanisms
24
SQL Tutorial
Quiesce Database
Portability
Self-managing database
SQL*Plus
ASM
Scheduler
Resource Manager
Data Warehousing
Materialized views
Bitmap indexes
Table compression
Parallel Execution
Analytic SQL
Data mining
Partitioning
MS ACCESS
Microsoft Access is one of the most popular Microsoft products. It is an entry-level
database management software. It is not only inexpensive but also a powerful database
for small-scale projects.
MS Access uses the Jet database engine, which utilizes a specific SQL language dialect
(sometimes referred to as Jet SQL). It comes with the professional edition of MS Office
package. MS Access has easy to-use intuitive graphical interface.
1993 - Access 1.1 released to improve compatibility with inclusion the Access Basic
programming language.
2007 - Access 2007, a new database format was introduced ACCDB which supports
complex data types such as multi valued and attachment fields.
2010 - Microsoft Access 2010 introduced a new version of the ACCDB format
supported hosting Access Web services on a SharePoint 2010 server.
2013 - Microsoft Access 2013 offers traditional Access desktop applications plus a
significantly updated SharePoint 2013 web service
Features
25
SQL Tutorial
Users can create tables, queries, forms and reports and connect them together with
macros.
Option of importing and exporting the data to many formats including Excel,
Outlook, ASCII, dBase, Paradox, FoxPro, SQL Server, Oracle, ODBC, etc.
There is also the Jet Database format (MDB or ACCDB in Access 2007), which can
contain the application and data in one file. This makes it very convenient to
distribute the entire application to another user, who can run it in disconnected
environments.
Microsoft Access offers parameterized queries. These queries and Access tables can
be referenced from other programs like VB6 and .NET through DAO or ADO.
The desktop editions of Microsoft SQL Server can be used with Access as an
alternative to the Jet Database Engine.
PostgreSQL
PostgreSQL is object relational database management system (ORDBMS) that supports
both relational (SQL) and non-relational (JSON) query. It is a highly stable database that
is developed based on the POSTGRES, version 4.2 released by the Computer Science
Department in University of Berkeley, California. This database is a popular database or
data warehouse for many applications.
History
1985 - A post-Ingres project, namely POSTGRES, has been initiated to address the
issues in the older database systems. This project aimed to fully support datatypes
and define them to establish relationships between the objects of the database.
POSTGRES was also able to retrieve data using rules.
1994 - With the increase in users, POSTGRES Version 4.2 was released, ending the
project with it.
1994 - Andrew Yu and Jolly Chen, students of University of Berkeley, replaced the
POSTQUEL query language that run POSTGRES, with the SQL language, creating
Postgres95.
1995 - Postgres95's first version was released with a more liberal license.
Since then, next versions of the project are released as free and open-source
software PostgreSQL License.
26
SQL Tutorial
2000 - PostgreSQL version 7.0 was released on July 8th, 2000. This version had
the most changes in features until then. Major changes made are as follows:
2005 - The version 8.0 was released on January 19th, 2005. Changes in this version
include, the server can now be run on Microsoft natively, savepoints were
introduced, Point-in-time recovery, tablespaces, improved buffer management,
checkpoints, changing column types was introduced, etc.
2017 - Version 10.0 was released on 5th October, 2017. Major improvements
include: logical replication, declarative table partitioning, improved query
parallelism, significant improvements in general performance, etc.
Features
Asynchronous Replication
Nested transactions
Online backups
Highly scalable.
SQLite
27
SQL Tutorial
History
2000 - SQLite 1.0 was released with GNU Database Manager. Dr. Richard Hipp
designed it in the same year to create a database that requires no administration.
2001 - SQLite 2.0 was released which contained a custom B-Tree implementation
instead of GNU Database Manager, adding transaction capability to the database.
2003 - SQLite 3.0 was released with major improvements like internalization,
manifest typing etc.
2018 - SQLite adopted the Code of Conduct from the Rule of Saint Benedict which
was later renamed as Code of Ethics due to some controversies.
Despite such controversies, newer versions of SQLite 3.x are being released
continuously with better features and improvements.
2023 - The latest version of SQLite is 3.42.0 which was released on 16th July,
2023.
Features
Transactions in SQLite are atomic, consistent, isolated and durable even when the
system crashes for any reason.
SQLite is server less, unlike other relational databases. It is linked with application
that accesses it. The application directly interacts with the SQLite to read and write
on files stored in the disk.
Enhanced Flexibility
28
SQL Tutorial
Relational databases utilize Data Definition Language (DDL) to modify data stored in tables
in real time. The most important thing users can easily add are new tables and columns,
rename, and implement various changes without disrupting ongoing database operations.
Data Consistency
Data Consistency is another important benefit of using SQL databases because it maintains
data consistency across applications and server instances.
Optimized Performance
Relational databases offer a range of value-added features that boast minimal memory
usage, reduced storage costs, and high processor speed.
Higher Compatibility
Relational databases offer higher compatibility for integration with modern technologies.
Scalability
Higher scalability is another feature that makes Relational databases most preferred.
==========
End of ebook preview
If you liked what you saw…
Buy it from our store @ https://www.tutorialspoint.com/index.htm
29