SQL
1. IMPORTANT COMMAND (not case sensitive):
SELECT - extracts data from a database
UPDATE - updates data in a database (use for records)
DELETE - deletes data from a database
INSERT INTO - inserts new data into a database
CREATE DATABASE - creates a new database
ALTER DATABASE - modifies a database (use for field)
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
DROP TABLE - deletes a table
CREATE INDEX - creates an index (search key)
DROP INDEX - deletes an index
2. SELECT used to select data from database
Example: SELECT field_name, field_name FROM table_name; (ALL= * )
3. SELECT DISTINCT used to return only distinct(different) values.
Example: select distinct address from supplier; (Supplier table 里面有的 address)
4. SELECT COUNT(DISTINCT field_name) from table_name used to find the number of
different records in the table.
Example: select count(distinct address) from supplier;
In MS Access databases: SELECT COUNT(*) AS Distinctaddress
FROM (SELECT DISTINCT address FROM suppplier);
5. WHERE used to filter records
Example: select * from table_name
WHERE Address= 'Johor';
Syntax: SELECT field_name
FROM table_name
WHERE condition;
6. ORDER BY used to sort the result-set in ascending or descending order
Example: select * from Product
ORDER BY Price;
Syntax: select field_name, field_name...
from table_name
order by field_name, field name ... ASC OR DESC;
Example: select* from Products
order by Price DESC;
Example: SELECT * FROM Customers
ORDER BY Country, CustomerName;
Example: SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
7. WHERE clause can contain one or many AND operators
Example: SELECT *
FROM Customers
WHERE Country = 'Spain' AND CustomerName LIKE 'G%';
Syntax: SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
8. AND & OR (The AND operator displays a record if all the conditions are TRUE. The
OR operator displays a record if any of the conditions are TRUE.)
Example: SELECT * FROM Customers
WHERE Country = 'Germany'
AND City = 'Berlin'
AND PostalCode > 12000;