Professional Documents
Culture Documents
SQL Tutorial
SQL Tutorial
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.
Also they are using different dialects, Such as:
Why SQL?
Allow
Allow
Allow
Allow
Allow
Allow
Allow
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
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:
Description
CREATE
ALTER
DROP
2|Page
Description
Creates a record
Modifies records
DELETE
Deletes records
Description
GRANT
REVOKE
Description
Retrieves certain records from one or more tables
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
4|Page
5|Page
FROM
TO
bigint
-9,223,372,036,854,775,808
9,223,372,036,854,775,807
int
-2,147,483,648
2,147,483,647
smallint
-32,768
32,767
tinyint
255
bit
decimal
-10^38 +1
10^38 .1
numeric
-10^38 +1
10^38 .1
money
-922,337,203,685,477.5808
+922,337,203,685,477.5807
smallmoney
-214,748.3648
+214,748.3647
FROM
TO
float
-1.79E + 308
1.79E + 308
real
-3.40E + 38
3.40E + 38
6|Page
FROM
TO
datetime
Jan 1, 1753
smalldatetime
Jan 1, 1900
Jun 6, 2079
date
time
Note: Here datetime has 3.33 milliseconds accuracy where as smalldatetime has 1 minute
accuracy.
FROM
TO
char
char
varchar
varchar
Maximum of 8,000
characters.(Variable-length nonUnicode data).
varchar(max)
varchar(max)
text
text
Description
nchar
nvarchar
nvarchar(max)
ntext
7|Page
Description
binary
varbinary
varbinary(max)
image
Description
sql_variant
timestamp
uniqueidentifier
xml
cursor
table
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 operation(s), such as comparisons and arithmetic operations.
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
Description
Example
a + b will give 30
b / a will give 2
b % a will give 0
Description
Example
(a = b) is not true.
!=
(a != b) is true.
<>
(a <> b) is true.
>
9|Page
(a < b) is true.
>=
<=
(a <= b) is true.
!<
(a !< b) is false.
!>
(a !> b) is true.
Description
ALL
The ALL operator is used to compare a value to all values in another value
set.
AND
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
IN
10 | P a g e
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.
OR
IS NULL
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:
11 | P a g e
SELECT numerical_expression as
[FROM table_name
WHERE CONDITION] ;
OPERATION_NAME
12 | P a g e
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:
13 | P a g e
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:
Syntax:
Basic syntax of USE statement is as follows:
USE DatabaseName;
Always database name should be unique within the RDBMS.
14 | P a g e
Syntax:
Basic syntax of CREATE TABLE statement is as follows:
Example:
15 | P a g e
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
Syntax:
There are two basic syntax of INSERT INTO statement is as follows:
Example:
Following statements would create six records in CUSTOMERS table:
17 | P a g e
+----+----------+-----+-----------+----------+
| 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:
18 | P a g e
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
Syntax:
The basic syntax of SELECT statement with WHERE clause is as follows:
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
+----+----------+----------+
| ID | NAME
| SALARY
|
+----+----------+----------+
| 5 | Hardik
| 8500.00 |
+----+----------+----------+
Syntax:
The basic syntax of AND operator with WHERE clause is as follows:
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
+----+-------+----------+
| 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:
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
+----+----------+----------+
| ID | NAME
| SALARY
|
+----+----------+----------+
| 3 | kaushik | 2000.00 |
| 4 | Chaitali | 6500.00 |
| 5 | Hardik
| 8500.00 |
| 6 | Komal
| 4500.00 |
| 7 | Muffy
| 10000.00 |
+----+----------+----------+
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];
You can combine N number of conditions using AND or OR operators.
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
+----+----------+-----+-----------+----------+
| 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 |
+----+----------+-----+---------+---------+
Syntax:
The basic syntax of DELETE query with WHERE clause is as follows:
24 | P a g e
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 DELETE 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 |
| 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:
Refer to the link http://www.tutorialspoint.com/sql
25 | P a g e
Learn JSP
Learn Servlets
Learn log4j
Learn iBATIS
Learn Java
Learn JDBC
Java Examples
Learn Best Practices
Learn Python
Learn Ruby
Learn Ruby on Rails
Learn SQL
Learn MySQL
Learn AJAX
Learn C Programming
Learn C++ Programming
Learn CGI with PERL
Learn DLL
Learn ebXML
Learn Euphoria
Learn GDB Debugger
Learn Makefile
Learn Parrot
Learn Perl Script
Learn PHP Script
Learn Six Sigma
Learn SEI CMMI
Learn WiMAX
Learn Telecom Billing
Learn ASP.Net
Learn HTML
Learn HTML5
Learn XHTML
Learn CSS
Learn HTTP
Learn JavaScript
Learn jQuery
Learn Prototype
Learn script.aculo.us
Web Developer's Guide
Learn RADIUS
Learn RSS
Learn SEO Techniques
Learn SOAP
Learn UDDI
Learn Unix Sockets
Learn Web Services
Learn XML-RPC
Learn UML
Learn UNIX
Learn WSDL
Learn i-Mode
Learn GPRS
Learn GSM
Learn WAP
Learn WML
Learn Wi-Fi
webmaster@TutorialsPoint.com
26 | P a g e