www.javadevhome.com

Google
   

SQL

ANSI-92 SQL
BNF tree format
W3 Schools - SQL Tutorial

Basic Commands

SELECT * FROM Persons
SELECT Company FROM Orders
SELECT DISTINCT Company FROM Orders
SELECT * FROM Persons WHERE FirstName='Tove'
SELECT * FROM Persons WHERE Year>1965
SELECT * FROM Persons WHERE Year IS NULL
SELECT * FROM Persons WHERE FirstName LIKE '%la%'
SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC, OrderNumber ASC
SELECT * FROM Persons WHERE (FirstName='Tove' OR FirstName='Stephen') AND LastName='Svendson'
SELECT * FROM Persons WHERE LastName IN ('Hansen','Pettersen')
SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen'
SELECT LastName AS Family, FirstName AS Name FROM Persons

SELECT Employees.Name FROM Employees, Orders WHERE Employees.Employee_ID=Orders.Employee_ID AND Orders.Product='Printer'
SELECT Employees.Name, Orders.Product FROM Employees INNER JOIN Orders ON Employees.Employee_ID=Orders.Employee_ID
SELECT Employees.Name, Orders.Product FROM Employees LEFT JOIN Orders ON Employees.Employee_ID=Orders.Employee_ID
SELECT Employees.Name, Orders.Product FROM Employees RIGHT JOIN Orders ON Employees.Employee_ID=Orders.Employee_ID

SELECT E_Name FROM Employees_Norway UNION SELECT E_Name FROM Employees_USA
SELECT E_Name FROM Employees_Norway UNION ALL SELECT E_Name FROM Employees_USA

SELECT wordID, word as catagory, (gloss + example) AS gloss2 INTO zTemp2 FROM englishExpr

INSERT INTO Persons (LastName, Address) VALUES ('Rasmussen', 'Storgt 67')

UPDATE Person SET Address = 'Stien 12', City = 'Stavanger' WHERE LastName = 'Rasmussen'

DELETE FROM Person WHERE LastName = 'Rasmussen'
DELETE * FROM table_name
DELETE FROM table_name

CREATE DATABASE database_name
CREATE TABLE Person (LastName varchar(30), FirstName varchar, Address varchar,Age int(3) )
CREATE VIEW [Products Above Average Price] AS SELECT ProductName,UnitPrice FROM Products WHERE UnitPrice>(SELECT AVG(UnitPrice) FROM Products)
CREATE VIEW [Category Sales For 1997] AS SELECT DISTINCT CategoryName,Sum(ProductSales) AS CategorySales FROM [Product Sales for 1997] GROUP BY CategoryName
CREATE UNIQUE INDEX PersonIndex ON Person (LastName DESC)
CREATE INDEX PersonIndex ON Person (LastName, FirstName)

ALTER TABLE Person ADD City varchar(30)
ALTER TABLE Person DROP COLUMN Address

DROP TABLE table_name
TRUNCATE TABLE table_name
DROP DATABASE database_name

SELECT COUNT(Age) FROM Persons
SELECT Company, SUM(Amount) FROM Sales
SELECT Company,SUM(Amount) FROM Sales GROUP BY Company
SELECT Company,SUM(Amount) FROM Sales GROUP BY Company HAVING SUM(Amount)>10000

SELECT LastName,Firstname INTO Persons_backup FROM Persons WHERE City='Sandnes'
SELECT Employees.Name, Orders.Product INTO Empl_Ord_backup FROM Employees INNER JOIN Orders ON Employees.Employee_ID=Orders.Employee_ID
SELECT Persons.* INTO Persons IN 'Backup.mdb' FROM Persons