SQL
SQL
in databases. Our SQL tutorial will teach you how to use SQL in: MySQL,
SQL Server, MS Access, Oracle, Sybase, Informix, Postgres, and other
database systems. SQL is a standard language for accessing and
manipulating databases
What is SQL?
Note: Most of the SQL database programs also have their own proprietary
extensions in addition to the SQL standard!
To build a web site that shows data from a database, you will need:
RDBMS
---------------------------------------------------------------------
--------------------------------------------------------------------
Syntax
Example Select all the different countries from the "Customers" table:
Count Distinct
Example
Example
The WHERE clause is used to filter records. It is used to extract only those
records that fulfill a specified condition.
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
You can use other operators than the = operator to filter the search.
Example Select all customers with a CustomerID greater than 80:
Operator Description
= Equal
Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
DESC
Order Alphabetically
Alphabetically DESC
The following SQL statement selects all customers from the "Customers"
table, sorted by the "Country" and the "CustomerName" column. This
means that it orders by Country, but if some rows have the same Country,
it orders them by CustomerName:
Example
Syntax
Example
You can combine the AND and OR operators. The following SQL statement
selects all customers from Spain that starts with a "G" or an "R". Make
sure you use parenthesis to get the correct result.
Example Select all Spanish customers that starts with either "G" or "R":
The following SQL statement selects all fields from Customers where
either City is "Berlin", CustomerName starts with the letter "G"
or Country is "Norway":
Example
SELECT * FROM Customers
WHERE City = 'Berlin' OR CustomerName LIKE 'G%' OR Country
= 'Norway';
The NOT operator is used in combination with other operators to give the
opposite result, also called the negative result.
Syntax
NOT LIKE
Example Select customers that does not start with the letter 'A':
NOT BETWEEN
NOT IN
Note: There is a not-greater-than operator: !> that would give you the
same result.
2. If you are adding values for all the columns of the table, you do not
need to specify the column names in the SQL query. However, make sure
the order of the values is in the same order as the columns in the table.
Here, the INSERT INTO syntax would be as follows:
INSERT INTO Example The following SQL statement inserts a new record
in the "Customers" table:
It is also possible to only insert data in specific columns. The following SQL
statement will insert a new record, but only insert data in the
"CustomerName", "City", and "Country" columns (CustomerID will be
updated automatically):
Example
Example
INSERT INTO Customers (CustomerName, ContactName, Address, City,
PostalCode, Country)
VALUES
('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway'),
('Greasy Burger', 'Per Olsen', 'Gateveien 15', 'Sandnes', '4306', 'Norway'),
('Tasty Tee', 'Finn Egan', 'Streetroad 19B', 'Liverpool', 'L1 0AA', 'UK');
Note: Make sure you separate each set of values with a comma ,.
Note: A NULL value is different from a zero value or a field that contains
spaces. A field with a NULL value is one that has been left blank during
record creation!
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;
The IS NULL operator is used to test for empty values (NULL values). The
following SQL lists all customers with a NULL value in the "Address" field:
Example
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
The IS NOT NULL operator is used to test for non-empty values (NOT NULL
values). The following SQL lists all customers with a value in the "Address"
field:
Example
UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
UPDATE Table
Example
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
It is the WHERE clause that determines how many records will be updated.
The following SQL statement will update the ContactName to "Juan" for all
records where country is "Mexico":
Example
UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';
DELETE Syntax
It is possible to delete all rows in a table without deleting the table. This
means that the table structure, attributes, and indexes will be intact:
The following SQL statement deletes all rows in the "Customers" table,
without deleting the table:
Example
Delete a Table
Example
The SELECT TOP clause is used to specify the number of records to return.
The SELECT TOP clause is useful on large tables with thousands of
records. Returning many records can impact performance.
Note: Not all database systems support the SELECT TOP clause. MySQL
supports the LIMIT clause to select a limited number of records, while
Oracle uses FETCH FIRST n ROWS ONLY and ROWNUM.
MySQL Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
LIMIT
The following SQL statement shows the equivalent example for MySQL:
FETCH FIRST
The following SQL statement shows the equivalent example for Oracle:
The following SQL statement selects the first 50% of the records from the
"Customers" table (for SQL Server/MS Access):
The following SQL statement selects the first three records from
the "Customers" table, where the country is "Germany" (for SQL
Server/MS Access):
Add the ORDER BY keyword when you want to sort the result, and return
the first 3 records of the sorted result.
1. SELECT MIN(Price)
FROM Products;
2. SELECT MAX(Price)
FROM Products;
Set Column Name (Alias) - When you use MIN() or MAX(), the returned
column will not have a descriptive name. To give the column a descriptive
name, use the AS keyword:
Group By:
Count()
1. SELECT COUNT(*)
FROM Products;
2. SELECT COUNT(ProductName)
FROM Products; (Note: If you specify a column name instead of (*),
NULL values will not be counted.)
3. SELECT COUNT(ProductID)
FROM Products
WHERE Price > 20;
Example: How many different prices are there in the Products table:
SUM:
1. SELECT SUM(Quantity)
FROM OrderDetails;
2. SELECT SUM(Quantity)
FROM OrderDetails
WHERE ProductId = 11;
3. SELECT SUM(Quantity) AS total
FROM OrderDetails;
4. SELECT OrderID, SUM(Quantity) AS [Total Quantity]
FROM OrderDetails
GROUP BY OrderID;
5. Sum() with an Expression: SELECT SUM(Quantity * 10)
FROM OrderDetails; (If we assume that each product in
the OrderDetails column costs 10 dollars, we can find the total
earnings in dollars by multiply each quantity with 10)
6. SELECT SUM(Price * Quantity)
FROM OrderDetails
LEFT JOIN Products ON OrderDetails.ProductID = Products.ProductID;