You are on page 1of 5

SQL is a standard language for accessing and manipulating databases.

What is SQL?
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL is an ANSI (American National Standards Institute) standard

What Can SQL do?
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in a database
SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database
SQL can create stored procedures in a database
SQL can create views in a database
SQL can set permissions on tables, procedures, and views
SQL is NOT case sensitive: SELECT is the same as select

Some of The Most Important SQL Commands
SELECT - extracts data from a database
UPDATE - updates data in a database
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
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




SELECT
SELECT CustomerName,City FROM Customers; selects the "CustomerName" and "City" columns
from the "Customers" table:
SELECT * FROM Customers; selects all the columns from the "Customers" table:
SELECT DISTINCT City FROM Customers; selects only the distinct values from the "City"
columns from the "Customers" table:

WHERE
SELECT * FROM Customers
WHERE Country='Mexico';
SELECT * FROM Customers
WHERE CustomerID=1;
Operator Description
= Equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN To specify multiple possible values for a column

AND&OR
SELECT * FROM Customers
WHERE Country='Germany'
AND (City='Berlin' OR City='Mnchen');

ORDER BY implicit e crescator
SELECT * FROM Customers
ORDER BY Country DESC
INSER INTO
A) INSERT INTO Customers
VALUES ('Cardinal','Tom B. Erichsen','Skagen
21','Stavanger','4006','Norway')

B) INSERT INTO Customers (CustomerID, CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway')

CustomerID CustomerName ContactName Address City PostalCode Country
87 Wartian Herkku Pirkko
Koskitalo
Torikatu 38 Oulu 90110 Finland
88 Wellington
Importadora
Paula Parente Rua do Mercado, 12 Resende 08737-363 Brazil
89 White Clover Markets Karl Jablonski 305 - 14th Ave. S. Suite
3B
Seattle 98128 USA
90 Wilman Kala Matti Karttunen Keskuskatu 45 Helsinki 21240 Finland
91 Wolski Zbyszek ul. Filtrowa 68 Walla 01-012 Poland
92 Cardinal Stavanger Norway


UPDATE Customers
SET ContactName='Alfred Schmidt', City='Hamburg'
WHERE CustomerName='Alfreds Futterkiste' AND Country='Germany'

DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste' AND ContactName='Maria Anders'

DELETE FROM table_name
or delete all rows in a table without deleting the table
DELETE * FROM table_name

Be very careful when deleting records. You cannot undo this statement!
TOP
TOP - Not all database systems support the TOP clause.
SELECT TOP 2 * FROM Customers; selects only the two first records
SELECT TOP 50 PERCENT * FROM Customers; selects the top 50% of the records

SQL SELECT TOP Equivalent in MySQL and Oracle
MySQL
SELECT * FROM Persons LIMIT 5
Oracle
SELECT * FROM Persons WHERE ROWNUM <=5
LIKE
LIKE - is used in a WHERE clause to search for a specified pattern in a column.
SELECT * FROM Customers
WHERE City LIKE 's%';
The "%" sign can be used to define wildcards (missing letters in the pattern) both before and after the
pattern.
SELECT * FROM Customers
WHERE Country LIKE '%den%'; selects the customers in a country containing the pattern "den"
SELECT * FROM Customers
WHERE Country NOT LIKE '%den%'; in a country NOT containing the pattern "den"
SQL Wildcards
SQL wildcards must be used with the SQL LIKE operator.
Wildcard Description
% A substitute for zero or more characters
_ A substitute for exactly one character
[charlist] Any single character in charlist
[^charlist]
or
[!charlist]
Any single character not in charlist
WHERE City LIKE 'ber%'; from a city that starts with "ber"
WHERE City LIKE '%es%'; in cities containing the pattern "es"
WHERE City LIKE 'L_n_on'; in the city that starts with a "L", followed by any character,
followed by "n", followed by any character, followed by "on"
WHERE City LIKE '[bsp]%'; in cities starting with "b", "s", or "p"
WHERE City LIKE '[!bsp]%'; in cities NOT starting with "b", "s", or "p"
IN
allows you to specify multiple values in a WHERE clause
SELECT * FROM Customers selects the companies in the cities "Paris" or "London"
WHERE City IN ('Paris','London');

You might also like