SQL stands for Structured Query Language SQL is pronounced sequel SQL is a declarative language SQL is used to access & manipulate data in databases Common SQL Databases are MS SQL Server, Oracle, DB2 and MySQL
Software that stores and manipulates data arranged in relational databasetables.
A set of data arranged in columns and rows.
SQL Commands Categories & Common Definitions
SQL consists of the following four Command Categories:
Data Query Language (DQL) SELECT - Retrieve data from table(s)
Data Manipulation Language (DML) INSERT - Insert data into db table UPDATE - Update data in db table DELETE - Delete data from table
Data Definition Language (DDL) CREATE - Create db object (table, view, etc.) ALTER - Modify db object (table, view, etc.) DROP - Delete db object (table, view, etc.)
Data Control Language (DCL) GRANT - Assign privilege REVOKE - remove privilege
Common DefinitionsSELECT Fields with an expression to query(fields are columns in table)
AS Alias name for a column to be returned
FROMTable to retrieve data from
WHERE Row level filter
GROUP BY Group when calculating aggregates
HAVING Group level filter
ORDER BY Alphabetical or numerical sortorder (ASC [ascending] or DESC [descending])
SELECT STATEMENTS EXAMPLES
Select statements enable us to view tables or selectedrecords and selected fields in a table or tables.
1. 1.
SELECT ,....FROM - Select data from specific field(s) in a table 2. SELECT * FROM - Selects everythingfrom the table 3. SELECT FROM ORDER BY, .....ASC- Select data fromfield(s) then sorts in ascending order 4. SELECT FROM ORDER BY, ....DESC- Select data fromfield(s) then sorts in descending order 5. SELECT FROM WHERE- Select data using WHERE clause for a given condition. SELECT ... SELECT ...
FROM WHERE OR - Select datausing WHERE clause with OR for two given conditions. 7. SELECT FROM WHEREAND - Select data using WHERE clausewith AND for two given conditions. 8. SELECT FROM WHEREBETWEEN AND - Select data usingWHERE clause using BETWEEN with AND for two given values. 9.
SELECT FROM WHERE LEFT(,1)IN ('E', 'G', 'H')- Select data using WHERE clause surname starts with E,G or H) 10. SELECT FROM WHEREIS NULL;- Select data using WHERE clause with NULL 11. SELECT FROM WHERELIKE 'A*' OR LIKE 'B*'- Select datausing WHERE clause with LIKE 12. SELECT TOP 10* FROM ORDER BY DESC - Select data using ORDER BY with TOP to query asorted list 13. SELECT * FROM WHERE = 'O'' Neil'- Select data using double quotes to cancel out 'single quotes'
INSERT, UPDATE & DELETE Data
Insert New DataINSERT INTO (,,.....)VALUES (, , ..... )- Inserts new data into a single table
Update Existing Data UPDATE SET = WHERE = Updates existing data in a single table
Delete Existing DataDELETE FROM WHERE =(usually an integer value)- Deletes ONE row of data froma table
DELETE * FROM - Deletes EVERYTHING froma table Be careful of this one!
Performing Calculations in a Query with MAX, SUM,AVG, MIN, COUNT, ROUND
1.
SELECT MAX() AS MaxField1,MAX() AS MaxField2 FROM 2. SELECT SUM() AS [TotalMarks] FROM 3. SELECT AVG() AS [AverageMark] FROM 4. SELECT MIN() AS [First First Name],MAX() AS [Last Surname] FROM 5. SELECT COUNT(*) AS [Number of Students] FROM 6. SELECT ROUND(AVG(YEAR(BDate)),2) AS AvgBDate FROM WHERE = 3
Joining Tables
1.
SELECT,,.... FROM,, WHERE = AND = This is a general SQL query combining TWO tables using WHEREclause 2. SELECT ,field2>, FROM INNER JOIN ON = (Alternative method) Returns all rows when there is at least one match in BOTHtables. 3. SELECT ,,FROM LEFT JOIN ON = General form of a LEFT JOIN Returns all rows from the lefttable, and the matched rows from the right table. 4.
SELECT ,,FROM RIGHT JOIN ON = General form of a RIGHT JOIN Returns all rows from the righttable, and the matched rows from the left table. 5. CREATE TABLE(,,,); General form for CREATING a table in a database
How to Order Data & Using Union
How to Order Data SELECT FROM WHERE ORDER BY
Example: SELECT FirstName, LastName, OrderDate FROM Orders WHERE OrderDate > '10/10/2010' ORDER BY OrderDate
Using Union SELECT FROM UNION SELECT FROM
Example: SELECT FirstName, LastName FROM Orders2010 UNION SELECT FirstName, LastName FROM Orders2011
SQL Cheat Sheet
t =table name,
c = column name,
vw = view name,
v = value,
dt= data type,
l = data type length,
at = table alias,
aggregate= aggregate function,
idx = index name
SQL SELECT STATEMENTS SELECT * FROM t SELECT c1,c2 FROM t SELECT c1,c2 FROM t WHERE conditions SELECT c1,c2 FROM t WHERE conditions ORDER BY c1 ASC,c2 DESC SELECT DISTICT c1,c2 FROM t SELECT c1, aggregate(c2 * c3) FROM t GROUP BY c1 SELECT c1, aggregate(c2 * c3) FROM t GROUP BY c1 HAVING c1 > v1
SQL OPERATORS SELECT * FROM t WHERE c1 [NOT] BETWEEN v1 AND v2 SELECT * FROM t WHERE c1 [NOT ] IN (v1,v2,) SELECT* FROM t WHERE c1 > v1 AND c1 < v2 SELECT * FROM t WHERE c1 < v1 OR c1 > v2 SELECT * FROM t WHERE c1 = v1 SELECT * FROM t WHERE c1 <> v1
SQL VIEW STATEMENTS CREATE UNIQUE INDEX idx ON t(c1,c2..) DROP INDEX t.idx
SQL JOIN STATEMENTS SELECT * FROM t1 INNER JOIN t2 ON conditions SELECT * FROM t WHERE c1 [NOT ] IN (v1,v2,) SELECT * FROM t1 INNER JOIN t2 ON conditions WHERE conditions SELECT * FROM t1, t2 WHERE conditions SELECT * FROM t1 LEFT JOIN t2 ON conditions SELECT * FROM t1 RIGHT JOIN t2 ON conditions SELECT * FROM t1 FULL OUTER JOIN t2 ON conditions SELECT * FROM t1 AS at1 INNER JOIN t2 AS at2 ON at1.c1 = at2.c2
SQL TABLE STATEMENTS SQL TABLE STATEMENTS CREATE TABLE t( c1 dt1(l1), c2 dt2(l2), ... ) DROP TABLE t ALTER TABLE t ADD COLUMN c dt(l) ALTER TABLE t DROP COLUMN c
SQL UPDATE DATABASE INSERT INTO t (c1,c2) VALUES (v1,v2) INSERT INTO t1 (c1,c2) SELECT c1,c2 FROM t2 WHERE conditions UPDATE t SET c1 = v1, c2 = v2, WHERE conditions DELETE FROM t WHERE conditions TRUNCATE TABLE t
SQL VIEW STATEMENTS CREATE VIEW vw AS SELECT c1,c2 FROM t ALTER VIEW vw AS SELECT c1,c2 FROM t DROP VIEW vw