Structured Query Language Is The Language Used by DBA and Programmers For Accessing Data From A Database
Structured Query Language Is The Language Used by DBA and Programmers For Accessing Data From A Database
and Programmers for accessing data from a Database. This language is used to
send request to Database and retrieve the required data. With SQL a developer
or user can write queries. DBMS processes and fetches the data from table(s)
and returns to the user.
SQL involves the process of requesting the data from a Database stored in
tables. What a user sends to DBMS is a Database Query written by following
the syntax and rules of structured Query Language.
SQL is the query language used to query data in all RDBMSs.
SQL is not a programming language and it doesn’t need to be compiled.
It is non-procedural language.
A programmer or developer doesn’t need to write programs like in C/C++,
Java etc. It is just like posing questions to the DBMS that fetches
required data rows matching the criteria from the database.
What is MySQL?
MySQL is a open source Relational Database Management System. MySQL is very fast reliable and flexible Database
Management System. It provides a very high performance and it is multi threaded and multi user Relational Database
management system.
MySQL Features
1. MySQL are very fast and much reliable for any type of application.
2. MySQL is very Lightweight application.
3. MySQL command line tool is very powerful and can be used to run SQL queries against database.
4. MySQL supports indexing and binary objects.
5. It is allow changes to structure of table while server is running.
6. MySQL has a wide user base.
7. It is a very fast thread-based memory allocation system.
8. MySQL Written in C and C++ language.
9. MySQL code is tested with different compilers.
10. MySQL is available as a separate program for use in a client/server network environment.
Advantages of MySql:
Reliability and Performance : MySQL is very reliable and high performance relational database management system.
It can used to store many GB's of data into database.
Availability of Source: MySQL source code is available that's why now you can recompile the source code.
Cross-Platform support: MySQL supports more then twenty different platform including the major
Linux distribution .Mac OS X, Unix and Microsoft windows.
Large pool of Trained and Certified Developers: MySQL is very popular and it is world most popular open source
Database. So it is easy to find high quality staff around the world.
Powerful Uncomplicated software: The MySQL has most capabilities to handle most corporate database application
and used to very easy and fast
Database Tables
A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders").
Tables contain records (rows) with data.
Below is an example of a table called "Persons":
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. The data in RDBMS is stored in
database objects called tables. A table is a collection of related data entries and it consists of columns and rows.
Number types:
SQL SELECT Statement
The SELECT statement is used to select data from a database. The result is stored in a result table, called the result-set. Syntax
SELECT column_name(s)
FROM table_name
and
LastNam FirstNam
e e
Hansen Ola
Svendson Tove
Pettersen Kari
SELECT * Example
Now we want to select all the columns from the "Persons" table. We use the following SELECT statement:
City
Sandnes
Stavange
r
SELECT *
FROM Persons
WHERE City='Sandnes'
The result-set will look like this:
This is correct:
SELECT * FROM Persons WHERE FirstName='Tove'
This is wrong:
SELECT * FROM Persons WHERE FirstName=Tove
For numeric values:
This is correct:
SELECT * FROM Persons WHERE Year=1965
This is wrong:
SELECT * FROM Persons WHERE Year='1965'
Operator Description
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEE
Between an inclusive range
N
LIKE Search for a pattern
IN If you know the exact value you want to return for at least one of the
columns
Note: In some versions of SQL the <> operator may be written as !=
The AND & OR Operators
The AND & OR operators are used to filter records based on more than one condition. The AND operator displays a
record if both the first condition and the second condition is true. And OR operator displays a record if either the first
condition or the second condition is true.
OR Operator Example
Now we want to select only the persons with the first name equal to "Tove" OR the first name equal to "Ola":
We use the following SELECT statement:
ORDER BY Example
The "Persons" table:
UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
WHERE LastName='Tjessem' AND FirstName='Jakob'
The "Persons" table will now look like this:
Try it Yourself
Test your SQL Skills
On this page you can test your SQL skills.
We will use the Customers table in the Northwind database:
To see how SQL works, you can copy the SQL statements below and paste them into the textarea, or you can make
your own SQL statements.
SQL LIKE Operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
SELECT *
FROM Persons
WHERE City LIKE 's%'
The "%" sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern.
The result-set will look like this:
P_I LastNam FirstNam
Address City
d e e
Timoteivn
1 Hansen Ola Sandnes
10
2 Svendson Tove Borgvn 23 Sandnes
Stavange
3 Pettersen Kari Storgt 20
r
Next, we want to select the persons living in a city that ends with an "s" from the "Persons" table.
We use the following SELECT statement:
SELECT *
FROM Persons
WHERE City LIKE '%s'
The result-set will look like this:
SELECT * FROM Persons
WHERE City LIKE '%tav
%'
The result-set will look like this:
SELECT * FROM Persons
WHERE City NOT LIKE '%tav
%'
The result-set will look like this:
SQL Wildcards
SQL wildcards can substitute for one or more characters when searching for data in a database.
SQL wildcards must be used with the SQL LIKE operator.
With SQL, the following wildcards can be used:
Wildcard Description
% A substitute for zero or more characters
_ A substitute for exactly one character
[charlist] Any single character in charlist
[^charlist] or [! Any single character not in charlist
charlist]
SQL Wildcard Examples
We have the following "Persons" table:
SELECT *
FROM Persons
WHERE City LIKE 'sa%'
The result-set will look like this:
SELECT * FROM Persons
WHERE City LIKE '%nes
%'
The result-set will look like this:
The IN Operator
The IN operator allows you to specify multiple values in a WHERE clause.
SQL IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)
IN Operator Example
The "Persons" table:
Example 2
To display the persons outside the range in the previous example, use NOT BETWEEN:
SQL Alias
You can give a table or a column another name by using an alias. This can be a good thing to do if you have very long
or complex table names or column names.
An alias name could be anything, but usually it is short.
The CREATE TABLE Statement
The CREATE TABLE statement is used to create a table in a database.
SQL Constraints
Constraints are used to limit the type of data that can go into a table. Constraints can be specified when a table is
created (with the CREATE TABLE statement) or after the table is created (with the ALTER TABLE statement). We will
focus on the following constraints:
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
DEFAULT
The next chapters will describe each constraint in details.
The ALTER TABLE Statement
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
SQL Functions
SQL has many built-in functions for performing calculations on data.
OrderAverag
e
950
Now we want to find the customers that have an OrderPrice value higher than the average OrderPrice value.
We use the following SQL statement:
Custome
r
Hansen
Nilsen
Jensen
SQL COUNT() Function
«
Next Chapter »
Previous
The COUNT() function returns the number of rows that matches a specified criteria.
CustomerNilse
n
2
NumberOfOrder
s
6
which is the total number of rows in the table.
NumberOfCustomer
s
3
which is the number of unique customers (Hansen, Nilsen, and Jensen) in the "Orders" table.
LargestOrderPric
e
2000
The MIN() Function
The MIN() function returns the smallest value of the selected column.
SmallestOrderPric
e
100
The SUM() Function
The SUM() function returns the total sum of a numeric column.
OrderTota
l
5700
The GROUP BY Statement
The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more
columns.
Custome SUM(OrderPrice
r )
Hansen 2000
Nilsen 1700
Jensen 2000
Nice! Isn't it? :)
Let's see what happens if we omit the GROUP BY statement:
Custome SUM(OrderPrice
r )
Hansen 5700
Nilsen 5700
Hansen 5700
Hansen 5700
Jensen 5700
Nilsen 5700
The result-set above is not what we wanted.
Explanation of why the above SELECT statement cannot be used: The SELECT statement above has two columns
specified (Customer and SUM(OrderPrice). The "SUM(OrderPrice)" returns a single value (that is the total sum of the
"OrderPrice" column), while "Customer" returns 6 values (one value for each row in the "Orders" table). This will
therefore not give us the correct result. However, you have seen that the GROUP BY statement solves this problem.
Custome SUM(OrderPrice
r )
Nilsen 1700
Now we want to find if the customers "Hansen" or "Jensen" have a total order of more than 1500.
We add an ordinary WHERE clause to the SQL statement:
Custome SUM(OrderPrice
r )
Hansen 2000
Jensen 2000
The UCASE() Function
The UCASE() function converts the value of a field to uppercase.
FirstNam
LastName
e
HANSEN Ola
SVENDSON Tove
PETTERSE
Kari
N
The LCASE() Function
The LCASE() function converts the value of a field to lowercase.
LastNam FirstNam
e e
hansen Ola
svendson Tove
pettersen Kari
The MID() Function
The MID() function is used to extract characters from a text field.
Parameter Description
column_name Required. The field to extract characters from
start Required. Specifies the starting position (starts at 1)
length Optional. The number of characters to return. If omitted, the MID()
function returns the rest of the text
SmallCit
y
Sand
Sand
Stav
The LEN() Function
The LEN() function returns the length of the value in a text field.
Parameter Description
column_name Required. The field to round.
decimals Required. Specifies the number of decimals to be
returned.
ProductNam UnitPric
e e
Jarlsberg 10
Mascarpone 33
Gorgonzola 16
The NOW() Function
The NOW() function returns the current system date and time.
ProductNam UnitPric
PerDate
e e
10/7/2008 11:25:02
Jarlsberg 10.45
AM
10/7/2008 11:25:02
Mascarpone 32.56
AM
10/7/2008 11:25:02
Gorgonzola 15.67
AM