My SQL
My SQL
WHAT IS SQL?
SQL IS THE STANDARD LANGUAGE FOR DEALING WITH RELATIONAL DATABASES.
SQL IS USED TO INSERT, SEARCH, UPDATE, AND DELETE DATABASE RECORDS.
SQL IS A STANDARD LANGUAGE FOR ACCESSING AND MANIPULATING DATABASES.
SQL STANDS FOR STRUCTURED QUERY LANGUAGE
SQL LETS YOU ACCESS AND MANIPULATE DATABASES
SQL BECAME A STANDARD OF THE AMERICAN NATIONAL STANDARDS INSTITUTE (ANSI) IN
1986, AND OF THE INTERNATIONAL ORGANIZATION FOR STANDARDIZATION (ISO) IN 1987
What Can SQL do?
Although SQL is an ANSI/ISO standard, there are different versions of the SQL language.
However, to be compliant with the ANSI standard, they all support at least the major commands
(such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.
Note: Most of the SQL database programs also have their own proprietary extensions in
addition to the SQL standard!
Using SQL in Your Web Site
To build a web site that shows data from a database, you will need:
An RDBMS database program (i.e. MS Access, SQL Server, MySQL)
To use a server-side scripting language, like PHP or ASP
To use SQL to get the data you want
To use HTML / CSS to style the page
RDBMS
2 Ana Trujillo Emparedados Ana Trujillo Avda. de la Constitución México D.F. 05021 Mexico
y helados 2222
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
7 Blondel père et fils Frédérique Citeaux 24, place Kléber Strasbourg 67000 France
Every table is broken up into smaller entities called fields. The fields in the Customers table consist of CustomerID,
CustomerName, ContactName, Address, City, PostalCode and Country. A field is a column in a table that is
designed to maintain specific information about every record in the table.
A record, also called a row, is each individual entry that exists in a table. For example, there are 91 records in the
above Customers table. A record is a horizontal entity in a table.
A column is a vertical entity in a table that contains all information associated with a specific field in a table.
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.
In this tutorial we will use the well-known Northwind sample database (included in MS Access and MS SQL
Server).
Below is a selection from the "Customers" table:
CustomerID CustomerName ContactName Address City PostalCode Country
3 Antonio Moreno Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
Taquería
SQL Statements
Most of the actions you need to perform on a database are done with SQL statements.
Example
SELECT
The following SQL * FROM
statement selectsCustomers;
all the records in the "Customers" table:
Keep in Mind That...
SQL keywords are NOT case sensitive: select is the same as SELECT
Semicolon after SQL Statements?
Some database systems require a semicolon at the end of each SQL statement.
Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL
statement to be executed in the same call to the server.
For example :
Select * from customer;
Here * is used to select all the data from customer table and display the entire record from it .
Select is the command to display the informations.
From will tell you that from where you have to select the data or information.
Some of The Most Important SQL Commands
3 Antonio Moreno Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
Taquería
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
The following SQL statement selects the "CustomerName", "City", and "Country" columns from the "Customers"
table:
Example
SELECT CustomerName, City, Country FROM Customers;
The SELECT DISTINCT statement is used to return only distinct (different) values.
Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different
(distinct) values.
SELECT DISTINCT Syntax:
SELECT DISTINCT column1, column2, ...
FROM table_name;
SELECT DISTINCT Examples
The following SQL statement selects only the DISTINCT values from the "Country" column in the "Customers"
table:
Example:
SELECT DISTINCT Country FROM Customers;
SELECT Example Without DISTINCT
The following SQL statement selects all (including the duplicates) values from the "Country" column in the
"Customers" table:
Example
SELECT Country FROM Customers;
The following SQL statement counts and returns the number of different (distinct) countries in the "Customers" table:
Example:
SELECT COUNT(DISTINCT Country) FROM Customers;
MySQL WHERE Clause
Note: The WHERE clause is not only used in SELECT statements, it is also used in UPDATE, DELETE, etc.!
Text Fields vs. Numeric Fields
SQL requires single quotes around text values (most database systems will also allow double quotes).
However, numeric fields should not be enclosed in quotes:
Example
SELECT * FROM Customers
WHERE CustomerID = 1;
Operator Description
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=
2 Chang 1 1 24 - 12 oz bottles 19
3 Aniseed Syrup 1 2 12 - 550 ml bottles 10
2 Chang 1 1 24 - 12 oz bottles 19
The following SQL statement selects all products with a price between 10 and 20:
Example
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
2 Chang 1 1 24 - 12 oz bottles 19
To display the products outside the range of the previous example, use NOT BETWEEN:
Example
SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
The following SQL statement selects all products with a price between 10 and 20. In addition; do not show products
with a CategoryID of 1,2, or 3:
Example
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20
AND CategoryID NOT IN (1,2,3);
The following SQL statement selects all products with a ProductName between Carnarvon Tigers and Mozzarella di
Giovanni:
Example
SELECT * FROM Products
WHERE ProductName BETWEEN 'Carnarvon Tigers' AND 'Mozzarella di Giovanni'
ORDER BY ProductName;
The following SQL statement selects all products with a ProductName between Carnarvon Tigers and Chef Anton's Cajun
Seasoning:
Example
SELECT * FROM Products
WHERE ProductName BETWEEN "Carnarvon Tigers" AND "Chef Anton's Cajun Seasoning"
ORDER BY ProductName;
NOT BETWEEN Text Values Example
The following SQL statement selects all products with a ProductName not between Carnarvon Tigers and
Mozzarella di Giovanni:
Example
SELECT * FROM Products
WHERE ProductName NOT BETWEEN 'Carnarvon Tigers' AND 'Mozzarella di Giovanni'
ORDER BY ProductName;
Sample table or Demo table
10248 90 5 7/4/1996 3
10249 81 6 7/5/1996 1
10250 34 4 7/8/1996 2
10251 84 3 7/9/1996 1
10252 76 4 7/10/1996 2
BETWEEN Dates Example
The following SQL statement selects all orders with an OrderDate between '01-July-1996' and '31-July-1996':
Example
SELECT * FROM Orders
WHERE OrderDate BETWEEN #07/01/1996# AND #07/31/1996#;
Or
Example
SELECT * FROM Orders
WHERE OrderDate BETWEEN '1996-07-01' AND '1996-07-31';
Between a certain range (BETWEEN)
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:
The percent sign (%) represents zero, one, or multiple characters
The underscore sign (_) represents one, single character
Note: MS Access uses an asterisk (*) instead of the percent sign (%), and a question mark (?) instead of the
underscore (_).
The percent sign and the underscore can also be used in combinations!
LIKE Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
Like operators
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2
characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least 3
characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with
"o"
SQL LIKE Examples
The following SQL statement selects all customers with a CustomerName starting with "a“:
Example
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';
The following SQL statement selects all customers with a CustomerName ending with "a":
Example
SELECT * FROM Customers
WHERE CustomerName LIKE '%a';
The following SQL statement selects all customers with a CustomerName that have "or" in any position:
Example
SELECT * FROM Customers
WHERE CustomerName LIKE '%or%';
SQL Like operators :
The following SQL statement selects all customers with a CustomerName that have "r" in the second position:
Example
SELECT * FROM Customers
WHERE CustomerName LIKE '_r%';
The following SQL statement selects all customers with a CustomerName that starts with "a" and are at least 3 characters in length:
Example
SELECT * FROM Customers
WHERE CustomerName LIKE 'a__%';
The following SQL statement selects all customers with a ContactName that starts with "a" and ends with "o":
Example
SELECT * FROM Customers
WHERE ContactName LIKE 'a%o';
The following SQL statement selects all customers with a CustomerName that does NOT start with "a":
Example
72 Seven Seas Imports Hari Kumar 90 Wadhurst Rd. London OX15 4NB UK
74 Spécialités du monde Dominique Perrier 25, rue Lauriston Paris 75016 France
The SQL IN Operator
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
7 Blondel père et fils Frédérique Citeaux 24, place Kléber Strasbourg 67000 France
The following SQL statement selects all customers that are located in "Germany", "France" or "UK":
Example
SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
The following SQL statement selects all customers that are NOT located in "Germany", "France" or "UK":
Example
SELECT * FROM Customers
WHERE Country NOT IN ('Germany', 'France', 'UK');
SQL NULL Values
It is not possible to test for NULL values with comparison operators, such as =, <, or <>. !=
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;
Example
SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NULL;
MySQL IFNULL function is one of the MySQL control flow functions that accepts two arguments and returns the
first argument if it is not NULL. Otherwise, the IFNULL function returns the second argument.
The two arguments can be literal values or expressions.
The following illustrates the syntax of the IFNULL function:
IFNULL(expression_1,expression_2);
CREATE TABLE contacts ( contactid INT AUTO_INCREMENT PRIMARY KEY, contactname
VARCHAR(20) NOT NULL, bizphone VARCHAR(15), homephone VARCHAR(15));
INSERT INTO contacts(contactname,bizphone,homephone)VALUES('John Doe','(541) 754-3009',NULL),
('Cindy Smith',NULL,'(541) 754-3110'), ('Sue Greenspan','(541) 754-3010','(541) 754-3011'), ('Lily
Bush',NULL,'(541) 754-3111');
SELECT contactName, bizphone, homephone FROM contacts;
SELECT contactname, IFNULL(bizphone, homephone) phone FROM contacts;