SQL Basics: Introduction To Standard Query Language
SQL Basics: Introduction To Standard Query Language
Introduction to
Standard Query Language
SQL What Is It?
Just like Excel tables, database tables consist of columns and rows.
Each column contains a different type of attribute and each row
corresponds to a single record. For example, imagine that we were
building a database table that contained names and telephone
numbers. We’d probably set up columns named “FirstName”,
“LastName” and “TelephoneNumber.” Then we’d simply start
adding rows underneath those columns that contained the data
we’re planning to store.
• SQL*Plus
• TOAD
• SQL Navigator
• Excel
• Access
• Lotus 1Lotus 1
• Heart of PL/SQL
Pros & Cons of SQL
Pros:
• Very flexible
• Universel (Oracle, Access, Paradox, etc)
• Relatively Few Commands to Learn
Cons:
• Requires Detailed Knowledge of the Structure of the Database
• Can Provide Misleading Results
Basic SQL Components
• SELECT schema.table.column
• FROM table alias
• WHERE [conditions]
• ORDER BY [columns];
Defines the end of an SQL statement
Some programs require it, some do not (TOAD Does Not)
Needed only if multiple SQL statements run in a script
Optional Elements
SELECT Statement
• SELECT Statement Defines WHAT is to be returned (separated by
commas)
Database Columns (From Tables or Views)
Constant Text Values
Formulas
Group Functions (COUNT, SUM, MAX, MIN, AVG)
• ““*” Mean All Columns From All Tables In the
• FROM Statement
• Example: SELECT roll_number, student_name
FROM Statement
SELECT *
FROM class
WHERE Clause
• Optional
• Defines what records are to be included in the query
• Uses Conditional Operators
– =, >, >=, <, <=, != (<>)=, (<>)
– BETWEEN x AND y
– IN (listlist)
– LIKE ‘‘%string%’’(“%” is a wild-card)
– IS NULLIS NULL
– NOT {BETWEEN / IN / LIKE / NULL}
• Multiple Conditions Linked with AND & OR Statements
• Strings Contained Within SINGLE QUOTES!!
AND & OR
Examples:
SELECT *
FROM class
ORDER BY roll_number DESC
Group Functions
• Performs Common Mathematical Operations on a Group of
Records
• Must define what Constitutes a Group by Using the GROUP BY
Clause
• All non-Group elements in the SELECT Statement Must be in the
GROUP BY Clause (Additional Columns are Optional) -
Examples:
SELECT *
FROM class
GROUP BU grade
OK, I understand How to Get Data
From 1 Table What about Multiple
Tables? Table…
Primary & Foreign Keys
Primary Keys
• 1 or More Columns Used to Uniquely Identify a record.
• All Columns Defined as PK’s MUST be populated
Foreign Keys
• Value on a table that references a Primary Key from a different
table
What are Joins?
A JOIN is a means for combining fields from two tables by using
values common to each.
types of JOINs:
• INNER
• OUTER
• LEFT
• RIGHT.
• Saves Typing
• Good Internal Documentation
• Better Headers
• If the same column name exists on multiple tables, SQL needs a way to
know which element you are referencing
Inner Join
Inner joins return all rows from multiple tables where the join
condition is met.
SELECT *
FROM employee INNER JOIN department ON employee.DepartmentID = department.DepartmentID
SELECT *
FROM employee, department
WHERE employee.DepartmentID = department.DepartmentID
Outer joins
An outer join does not require each record in the two joined tables to have
a matching record. The joined table retains each record—even if no
other matching record exists.
Outer joins subdivide further into left outer joins, right outer joins, and full
outer joins
Left outer join
The result of a left outer join (or simply left join) for table A and B always
contains all records of the "left" table (A), even if the join-condition does
not find any matching record in the "right" table (B).
This means that if the ON clause matches 0 (zero) records in B, the join will
still return a row in the result—but with NULL in each column from B.
This means that a left outer join returns all the values from the left table,
plus matched values from the right table (or NULL in case of no
matching join predicate). If the right table returns one row and the left
table returns more than one matching row for it, the values in the right
table will be repeated for each distinct row on the left table.
Left outer join
SELECT *
FROM employee LEFT OUTER JOIN department ON employee.DepartmentID =
department.DepartmentID
SELECT *
FROM employee, department
WHERE employee.DepartmentID = department.DepartmentID(+)
Right outer join
A right outer join (or right join) closely resembles a left outer join,
except with the treatment of the tables reversed. Every row from
the "right" table (B) will appear in the joined table at least once.
If no matching row from the "left" table (A) exists, NULL will appear
in columns from A for those records that have no match in B. A
right outer join returns all the values from the right table and
matched values from the left table (NULL in case of no matching
join predicate).
For example, this allows us to find each employee and his or her
department, but still show departments that have no employees.
Right outer join
SELECT *
FROM employee RIGHT OUTER JOIN department ON employee.DepartmentID =
department.DepartmentID
You do the research on:
What is: