SQL Tutorial PDF
SQL Tutorial PDF
SQL Tutorial PDF
SQL Tutorial
Tutorialspoint.com
SQL is a database computer language designed for the retrieval and management of
data in relational database.
SQL stands for Structured Query Language. This tutorial gives an initial push to start
you with SQL. For more detail kindly check tutorialspoint.com/sql
This SQL tutorial gives unique learning on Structured Query Language and it helps to make
practice on SQL commands which provides immediate results. SQL is a language of database, it
includes database creation, deletion, fetching rows and modifying rows etc.
SQL is an ANSI (American National Standards Institute) standard but there are many different
versions of the SQL language.
What is SQL?
SQL is structured Query Language which is a computer language for storing, manipulating and
retrieving data stored in relational database.
SQL is the standard language for Relation Database System. All relational database
management systems like MySQL, MS Access, Oracle, Sybase, Informix, postgres and SQL
Server uses SQL as standard database language.
Why SQL?
History:
1970 -- Dr. E.F. "Ted" of IBM is known as the father of relational databases. He
described a relational model for databases.
1974 -- Structured Query Language appeared.
1978 -- IBM worked to develop Codd's ideas and released a product named System/R.
1986 -- IBM developed the first prototype of relational database and standardized by
ANSI. The first relational database was released by Relational Software and its later
becoming Oracle.
SQL Process:
1|Page
Tutorials Point, Simply Easy Learning
When you are executing an SQL command for any RDBMS, the system determines the best way
to carry out your request and SQL engine figures out how to interpret the task.
There are various components included in the process. These components are Query Dispatcher,
Optimization engines, Classic Query Engine and SQL query engine etc. Classic query engine
handles all non-SQL queries but SQL query engine won't handle logical files.
SQL Commands:
The standard SQL commands to interact with relational databases are CREATE, SELECT,
INSERT, UPDATE, DELETE, and DROP. These commands can be classified into groups based on
their nature:
2|Page
Tutorials Point, Simply Easy Learning
SQL –Syntax
SQL is followed by 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 and all the statements end with a semicolon (;).
Important point to be noted is that SQL is case insensitive which means SELECT and select
have same meaning in SQL statements but MySQL make difference in table names. So if you
are working with MySQL then you need to give table names as they exist in the database.
SQL IN Clause:
SELECT column1, column2....columnN
FROM table_name
WHERE column_name IN (val-1, val-2,...val-N);
3|Page
Tutorials Point, Simply Easy Learning
4|Page
Tutorials Point, Simply Easy Learning
5|Page
Tutorials Point, Simply Easy Learning
SQL data type is an attribute that specifies type of data of any object. Each column, variable
and expression has related data type in SQL.
You would use these data types while creating your tables. You would choose a particular data
type for a table column based on your requirement.
SQL Server offers six categories of data types for your use:
tinyint 0 255
bit 0 1
6|Page
Tutorials Point, Simply Easy Learning
Note: Here datetime has 3.33 milliseconds accuracy where as smalldatetime has 1 minute
accuracy.
7|Page
Tutorials Point, Simply Easy Learning
varbinary(max) Maximum length of 231 bytes (SQL Server 2005 only). ( Variable
length Binary data)
xml Stores XML data. You can store xml instances in a column or a
variable (SQL Server 2005 only).
SQL – Operators
What is an Operator in SQL?
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
8|Page
Tutorials Point, Simply Easy Learning
Show Examples
Show Examples
9|Page
Tutorials Point, Simply Easy Learning
Show Examples
Operator Description
ALL The ALL operator is used to compare a value to all values in another value
set.
AND The AND operator allows the existence of multiple conditions in an SQL
statement's WHERE clause.
ANY The ANY operator is used to compare a value to any applicable value in the
list according to the condition.
BETWEEN The BETWEEN operator is used to search for values that are within a set of
values, given the minimum value and the maximum value.
EXISTS The EXISTS operator is used to search for the presence of a row in a
specified table that meets certain criteria.
10 | P a g e
Tutorials Point, Simply Easy Learning
LIKE The LIKE operator is used to compare a value to similar values using
wildcard operators.
NOT The NOT operator reverses the meaning of the logical operator with which it
is used. Eg. NOT EXISTS, NOT BETWEEN, NOT IN etc. This is negate
operator.
IS NULL The NULL operator is used to compare a value with a NULL value.
UNIQUE The UNIQUE operator searches every row of a specified table for uniqueness
(no duplicates).
SQL – Expressions
An expression is a combination of one or more values, operators, and SQL functions that
evaluate to a value.
SQL EXPRESSIONs are like formulas and they are written in query language. You can also used
to query the database for specific set of data.
Syntax:
Consider the basic syntax of the SELECT statement as follows:
There are different types of SQL expression, which are mentioned below:
11 | P a g e
Tutorials Point, Simply Easy Learning
There are several built-in functions like avg(), sum(), count() etc.to perform what is known as
aggregate data calculations against a table or a specific table column.
12 | P a g e
Tutorials Point, Simply Easy Learning
Syntax:
Basic syntax of CREATE DATABASE statement is as follows:
Example:
If you want to create new database <testDB>, then CREATE DATABASE statement would be as
follows:
Make sure you has admin previledge before creating any database. Once a database is created,
you can check it in the list of databases as follws:
13 | P a g e
Tutorials Point, Simply Easy Learning
Syntax:
Basic syntax of DROP DATABASE statement is as follows:
Example:
If you want to delete an existing database <testDB>, then DROP DATABASE statement would
be as follows:
NOTE: Be careful before using this operation because by deleting an existing database would
result in loss of complete information stored in the database.
Make sure you has admin previledge before dropping any database. Once a database is
dropped, you can check it in the list of databases as follws:
The SQL USE statement is used to select any existing database in SQL schema.
Syntax:
Basic syntax of USE statement is as follows:
USE DatabaseName;
14 | P a g e
Tutorials Point, Simply Easy Learning
Example:
You can check available databases as follows:
Now if you want to work with AMROOD database then you can execute following SQL command
and start working with AMROOD database:
Syntax:
Basic syntax of CREATE TABLE statement is as follows:
CREATE TABLE is the keyword telling the database system what you want to do.in this case, you
want to create a new table. The unique name or identifier for the table follows the CREATE
TABLE statement.
Then in brackets comes the list defining each column in the table and what sort of data type it
is. The syntax becomes clearer with an example below.
A copy of an existing table can be created using a combination of the CREATE TABLE statement
and the SELECT statement. You can check complete detail at Create Table Using another Tables
Example:
15 | P a g e
Tutorials Point, Simply Easy Learning
Following is an example which creates a CUSTOMERS table with ID as primary key and NOT
NULL are the constraints showing that these fileds can not be NULL while creating records in this
table:
You can verify if your table has been created successfully by looking at the message displayed
by the SQL server otherwise you can use DESC command as follows:
Now you have CUSTOMERS table available in your database which you can use to store required
information related to customers.
NOTE: You have to be careful while using this command because once a table is deleted then all
the information available in the table would also be lost forever.
Syntax:
Basic syntax of DROP TABLE statement is as follows:
Example:
Let us first verify CUSTOMERS table, and then we would delete it from the database:
16 | P a g e
Tutorials Point, Simply Easy Learning
| NAME | varchar(20) | NO | | | |
| AGE | int(11) | NO | | | |
| ADDRESS | char(25) | YES | | NULL | |
| SALARY | decimal(18,2) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
This means CUSTOMERS table is available in the database, so let us drop it as follows:
Now if you would try DESC command then you would get error as follows:
Here TEST is database name which we are using for our examples.
Syntax:
There are two basic syntax of INSERT INTO statement is as follows:
Here column1, column2,...columnN are the names of the columns in the table into which you
want to insert data.
You may not need to specify the column(s) name in the SQL query if you are adding values for
all the columns of the table. But make sure the order of the values is in the same order as the
columns in the table. The SQL INSERT INTO syntax would be as follows:
Example:
Following statements would create six records in CUSTOMERS table:
17 | P a g e
Tutorials Point, Simply Easy Learning
You can create a record in CUSTOMERS table using second syntax as follows:
All the above statement would product following records in CUSTOMERS table:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Syntax:
The basic syntax of SELECT statement is as follows:
Here column1, column2...are the fields of a table whose values you want to fetch. If you want
to fetch all the fields available in the field then you can use following syntax:
18 | P a g e
Tutorials Point, Simply Easy Learning
Example:
Consider CUSTOMERS table is having following records:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Following is an example which would fetch ID, Name and Salary fields of the customers
available in CUSTOMERS table:
+----+----------+----------+
| ID | NAME | SALARY |
+----+----------+----------+
| 1 | Ramesh | 2000.00 |
| 2 | Khilan | 1500.00 |
| 3 | kaushik | 2000.00 |
| 4 | Chaitali | 6500.00 |
| 5 | Hardik | 8500.00 |
| 6 | Komal | 4500.00 |
| 7 | Muffy | 10000.00 |
+----+----------+----------+
If you want to fetch all the fields of CUSTOMERS table then use the following query:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
19 | P a g e
Tutorials Point, Simply Easy Learning
If the given condition is satisfied then only it returns specific value from the table. You would
use WHERE clause to filter the records and fetching only necessary records.
The WHERE clause not only used in SELECT statement, but it is also used in UPDATE, DELETE
statement etc. which we would examine in subsequent chapters.
Syntax:
The basic syntax of SELECT statement with WHERE clause is as follows:
You can specify a condition using comparision or logical operators like >, <, =, LIKE, NOT etc.
Below examples would make this concept clear.
Example:
Consider CUSTOMERS table is having following records:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Following is an example which would fetch ID, Name and Salary fields from the CUSTOMERS
table where salary is greater than 2000:
+----+----------+----------+
| ID | NAME | SALARY |
+----+----------+----------+
| 4 | Chaitali | 6500.00 |
| 5 | Hardik | 8500.00 |
| 6 | Komal | 4500.00 |
| 7 | Muffy | 10000.00 |
20 | P a g e
Tutorials Point, Simply Easy Learning
+----+----------+----------+
Following is an example which would fetch ID, Name and Salary fields from the CUSTOMERS
table for a customer with name Hardik. Here it is important to note that all the strings should
be given inside single quotes ('') where as numeric values should be given without any quote as
in above example:
+----+----------+----------+
| ID | NAME | SALARY |
+----+----------+----------+
| 5 | Hardik | 8500.00 |
+----+----------+----------+
These operators provide a means to make multiple comparisons with different operators in the
same SQL statement.
Syntax:
The basic syntax of AND operator with WHERE clause is as follows:
You can combine N number of conditions using AND operator. For an action to be taken by the
SQL statement, whether it be a transaction or query, all conditions separated by the AND must
be TRUE.
Example:
Consider CUSTOMERS table is having following records:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
21 | P a g e
Tutorials Point, Simply Easy Learning
Following is an example which would fetch ID, Name and Salary fields from the CUSTOMERS
table where salary is greater than 2000 AND age is less tan 25 years:
+----+-------+----------+
| ID | NAME | SALARY |
+----+-------+----------+
| 6 | Komal | 4500.00 |
| 7 | Muffy | 10000.00 |
+----+-------+----------+
The OR Operator:
The OR operator is used to combine multiple conditions in an SQL statement's WHERE clause.
Syntax:
The basic syntax of OR operator with WHERE clause is as follows:
You can combine N number of conditions using OR operator. For an action to be taken by the
SQL statement, whether it be a transaction or query, only any ONE of the conditions separated
by the OR must be TRUE.
Example:
Consider CUSTOMERS table is having following records:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
22 | P a g e
Tutorials Point, Simply Easy Learning
+----+----------+-----+-----------+----------+
Following is an example which would fetch ID, Name and Salary fields from the CUSTOMERS
table where salary is greater than 2000 OR age is less tan 25 years:
+----+----------+----------+
| ID | NAME | SALARY |
+----+----------+----------+
| 3 | kaushik | 2000.00 |
| 4 | Chaitali | 6500.00 |
| 5 | Hardik | 8500.00 |
| 6 | Komal | 4500.00 |
| 7 | Muffy | 10000.00 |
+----+----------+----------+
You can use WHERE clause with UPDATE query to update selected rows otherwise all the rows
would be effected.
Syntax:
The basic syntax of UPDATE query with WHERE clause is as follows:
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
Example:
Consider CUSTOMERS table is having following records:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
23 | P a g e
Tutorials Point, Simply Easy Learning
Following is an example which would update ADDRESS for a customer whose ID is 6:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | Pune | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
If you want to modify all ADDRESS and SALARY column values in CUSTOMERS table, you do not
need to use WHERE clause and UPDATE query would be as follows:
+----+----------+-----+---------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+---------+---------+
| 1 | Ramesh | 32 | Pune | 1000.00 |
| 2 | Khilan | 25 | Pune | 1000.00 |
| 3 | kaushik | 23 | Pune | 1000.00 |
| 4 | Chaitali | 25 | Pune | 1000.00 |
| 5 | Hardik | 27 | Pune | 1000.00 |
| 6 | Komal | 22 | Pune | 1000.00 |
| 7 | Muffy | 24 | Pune | 1000.00 |
+----+----------+-----+---------+---------+
You can use WHERE clause with DELETE query to delete selected rows, otherwise all the records
would be deleted.
Syntax:
The basic syntax of DELETE query with WHERE clause is as follows:
24 | P a g e
Tutorials Point, Simply Easy Learning
WHERE [condition];
Example:
Consider CUSTOMERS table is having following records:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
If you want to DELETE all the records from CUSTOMERS table, you do not need to use WHERE
clause and DELETE query would be as follows:
Further Detail:
25 | P a g e
Tutorials Point, Simply Easy Learning
26 | P a g e