More Related Content
Similar to sqltutorialbasiccommands-130310014513-phpapp01.pdf
Similar to sqltutorialbasiccommands-130310014513-phpapp01.pdf (20)
sqltutorialbasiccommands-130310014513-phpapp01.pdf
- 2. Agenda
• Database Basics
• SQL Commands
– SELECT … FROM
– WHERE
– ORDER BY
– GROUP BY
– HAVING
© 2013 1keydata.com All Rights Reserved
- 6. SELECT … FROM
© 2013 1keydata.com All Rights Reserved
SELECT “COLUMN_NAME”
FROM “TABLE_NAME”
SQL is structured similar to the English language. The basic
command for retrieving data from a database table is to
SELECT data FROM a table. Not surprisingly, the keywords
"SELECT" and "FROM" make up the core of a SQL
statement.
The syntax for “SELECT… FROM” is:
- 7. SELECT … FROM
© 2013 1keydata.com All Rights Reserved
Select more than 1 column:
SELECT “COLUMN_NAME_1”, “COLUMN_NAME_2”
FROM “TABLE_NAME”
Select all columns:
SELECT *
FROM “TABLE_NAME”
Different ways of selecting data:
Select unique values:
SELECT DISTINCT “Column_Name”
FROM “TABLE_NAME”
- 8. WHERE
SELECT “COLUMN_NAME”
FROM “TABLE_NAME”
WHERE “CONDITION”
© 2013 1keydata.com All Rights Reserved
Sometimes we want to retrieve only a subset of the data. In
those cases, we use the “WHERE” keyword.
The syntax for “WHERE” is:
CONDITION represents how we want the data to be filtered.
- 9. ORDER BY
SELECT “COLUMN_NAME”
FROM “TABLE_NAME”
WHERE “CONDITION”
ORDER BY “COLUMN_NAME” [ASC | DESC]
© 2013 1keydata.com All Rights Reserved
When we want to list the results in a particular order
(ascending or descending), we use the ORDER BY keyword at
the end of the SQL statement.
The syntax for “ORDER BY” is:
- 10. MATHEMATICAL FUNCTIONS
© 2013 1keydata.com All Rights Reserved
SQL has built-in mathematical functions to allow us to
perform mathematical operations on the data. Common
mathematical functions include:
• SUM
• AVG
• COUNT
• MAX
• MIN
- 11. GROUP BY
© 2013 1keydata.com All Rights Reserved
Date Store Sales_Amount
SELECT MAX(Sales_Amount)
FROM SALES_HISTORY;
To find the highest Sales_Amount across all stores, we use the
MAX( ) function in the following SQL:
SALES_HISTORY
- 12. GROUP BY
© 2013 1keydata.com All Rights Reserved
Date Store Sales_Amount
SELECT Store, MAX(Sales_Amount)
FROM SALES_HISTORY;
To find the highest Sales_Amount for each store, we change
the SELECT portion to include “Store”:
SALES_HISTORY
- 13. GROUP BY
© 2013 1keydata.com All Rights Reserved
Date Store Sales_Amount
SELECT Store, MAX(Sales_Amount)
FROM SALES_HISTORY
GROUP BY Store;
However, this SELECT statement by itself is not enough. To
allow SQL to correctly calculate what we want, we need to use
the GROUP BY keyword. In the following example, the Store
column after GROUP BY tells SQL to apply the MAX
function for each Store.
SALES_HISTORY
- 15. HAVING
© 2013 1keydata.com All Rights Reserved
Previously we had talked about using the WHERE keyword to
filter results.
We cannot use WHERE to filter based on the result of a
function, because we need to specify the filtering condition
after SQL has calculated the function, and consequently any
filtering condition based on the function needs to be specified
after the GROUP BY phrase. So we cannot use the WHERE
keyword because it is always used before GROUP BY.
HAVING is used to filter based on the result of a function.
- 17. HAVING
© 2013 1keydata.com All Rights Reserved
Date Store Sales_Amount SELECT Store, SUM(Sales_Amount)
FROM SALES_HISTORY
GROUP BY Store
HAVING SUM(Sales_Amount) > 100;
Using the SALES_HISTORY table we had earlier. If we want
to sum the sales amount for each store, but only want to see
results for stores with total sales amount greater than 100, we
use the following SQL:
SALES_HISTORY
- 18. Order of SQL Commands
A SELECT statement has the following order:
• SELECT … FROM
• WHERE
• GROUP BY
• HAVING
• ORDER BY
© 2013 1keydata.com All Rights Reserved