SQL: Just The Basics: Thursday, February 05, 2009
SQL: Just The Basics: Thursday, February 05, 2009
What is SQL?
SQL used be "SEQUEL" and stood for 'Structured English Query Language', but it
was changed to SQL and now stands for 'Structured Query Language.'
Supposedly, the change was due to a trademark infringement with another
company.
SQL is what users like you and I use to access and manipulate databases.
SQL can....
• execute queries against a database
• etc.
SQL is just a general term used to describe the language used to interact with
databases.
There are in fact several variations of SQL that are widely used; such as,
SQL/PSM -- SQL/Persistent Stored Modules (ANSI/ISO Standard)
PSQL -- Procedural SQL (Interbase/Firebird)
SQL PL -- SQL Procedural Language (IBM)
T-SQL -- Transact SQL (Microsoft/Sybase)
SQL/PSM - SQL/Persistent Stored Module (MySQL)
PL/SQL -- Procedural Language/SQL (Oracle)
PL/pgSQL -- Procedural Language/PostgreSQL Structured Query Language
(PostgreSQL)
PL/PSM -- Procedural Language/Persistent Stored Modules (PostgreSQL)
I won't go into detail because that is for a later date and a different post.
Before we get started, let me explain that I will only be covering the DML part of
SQL. To refresh your memory, DML stands for Data Manipulation Language. We'll
probably go over the DDL (Data Definition Language) part later.
These tables will be used in each exercise to demonstrate how each SQL
command works.
In nearly every query, the commands SELECT and FROM will always be used.
Why? Because in order to retrieve data from databases, we need to SELECT what
it is that we want to see and need to specify FROM which table(s) to retrieve the
data.
SELECT
This statement is used to select data from a database.
SELECT table_name.column_name(s)
FROM table_name;
**NOTE:
1. SQL is not case sensitive, but it is good practice to keep all caps for easy readability.
2. 'FROM' does not have to be on a separate line; however, keeping it on a separate line is considered
best practice due easy readability.
3. Placing the name of the table with the column name is best practice because future queries will
require joining tables and tables may have the same column name and the only way the query can
distinguish between columns with similar names is by indicating what tables the columns belong
to.
An example:
From the Employee table, select the last name, first name, and occupation columns.
FROM Employee;
SELECT DISTINCT
This statement is used when selecting only distinct, meaning non-repetitive, unique, data
from a table.
From the Employee table, select distinct values from the Location’s column.
WHERE
This clause is used to select records that meet a specific condition(s).
SELECT table_name.column_name(s)
FROM table_name
WHERE table_name.column_name 'operator value';
An example of WHERE:
From the Employee table column, select all of the columns where the Location column is
equal to Minneapolis.
SELECT *
FROM Employee
WHERE Employee.Location = ‘Minneapolis’;
**NOTE: When the condition references a text value, then you must enclose the condition with single quotes;
however, numerical values do not require single quotes.
AND & OR
These operators are used when there are more than 1 condition in the query. They can be used
separately or together.
SELECT table_name.column_name(s)
FROM table_name
WHERE table_name.column_name 'operator value' AND table_name.column_name
'operator value';
(This query means that BOTH conditions must be met in order for the select value(s) to
appear in the result-set. If one condition is not met, then no value(s) will appear in the result-
set.)
SELECT table_name.column_name(s)
FROM table_name
WHERE table_name.column_name 'operator value' OR table_name.column_name 'operator
value';
(This query will return value(s) provided that either one of the two conditions are met. For
instance, if the condition is column_name = 'red' OR column_name = 'blue' and only the first
condition can be met, then the result-set will show the rows that meet the first condition.)
SELECT column_name(s)
FROM table_name
WHERE table_name.column_name 'operator value' AND (table_name.column_name
'operator value' OR table_name.column_name 'operator value');
(This query combines both AND & OR resulting in a result-set that must still meet both
conditions, but with the second condition containing a separate condition that does or does
not have to meet the conditions.)
SELECT *
FROM Employee
The BETWEEN operator is used when selecting a range of data that is between two values.
SELECT table_name.column_name(s)
FROM table_name
WHERE table_name.column_name IN ('value', value, 'value', etc);
NOTE: The conditional portion of the query could also be written using OR, but would make the query look
long: WHERE tale_name. column_name = 'value' OR table_name.column_name = value OR
table_name.column_name ='value' etc. Using IN requires less typing and makes the query look more clean and
intelligible.
SELECT table_name.column_name(s)
FROM table_name
WHERE table_name.column_name BETWEEN value AND value;
NOTE: The conditional portion of the query can also be rewritten using AND; for example, WHERE
table_name.column_name >= value AND table_name.column_name <= value, where 'value' is a numerical
value.
From the Employee table, select all employees with the LastName equal to Alba, Bower or
Davis.
SELECT *
FROM Employee
From the Employee table, select all employees whose Location is between Minneapolis and
Minneapolis.
SELECT *
FROM Employee
Note: Depending on the database that you are using, the BETWEEN function will work different. It may include
or exclude the test values.
SELECT table_name.column_name(s)
FROM table_name
ORDER BY table_name.column_name(s) ASC / DESC;
Select all the employees and rank them by Location in ascending order.
SELECT *
FROM Employee
SELECT AVG(table_name.column_name)
FROM table_name
WHERE table_name.column_name 'operator value';
SELECT COUNT(table_name.column_name)
FROM table_name;
SELECT COUNT(*)
FROM table_name;
(**This will return the number of rows for the selected table**)
SELECT AVG(Salary.Salary)
FROM Salary;
Note: In the result-set, the name of the column is AVG(Salary.Salary). To give the column name a more
appropriate title, you can give the column name an alias. For instance, SELECT AVG(Salary.Salary) AS Avg.
Salary. The column header will now appear as Avg. Salary.
GROUP BY
This statement is used to group the result-set by one of more columns and is used in
conjunction with the aggregate functions mentioned above.
Create a list of the total salaries of employees and group them by occupation.
FROM Salary
GROUP BY Occupation;
HAVING
This clause is used in conjunction with ORDER BY and places a condition on the column(s)
in the GROUP BY clause. Also, this clause is used with ORDER BY because WHERE
cannot be used with aggregate functions.
An example of HAVING:
Find the occupations that have a total salary less than 200000.
FROM Salary
GROUP BY Salary.Occupation
What I've shared with you now is just the tip of the SQL iceberg.
Continue to check back on this blog post as I will continue to update it with additions and
edits.