SQL 1st
SQL 1st
SQL 1st
SQL
• SQL stands for Structured Query Language. It is used for storing and
managing data in relational database management system (RDMS).
• It is a standard language for Relational Database System. It enables a
user to create, read, update and delete relational databases and tables.
• All the RDBMS like MySQL, Informix, Oracle, MS Access and SQL
Server use SQL as their standard database language.
• SQL allows users to query the database in a number of ways, using
English-like statements.
Advantages of SQL
• SQL Datatype is used to define the values that a column can contain.
• Every column is required to have a name and data type in the
database table.
1. Binary Datatypes
2. Approximate Numeric Datatype :
3. Exact Numeric Datatype
4. Character String Datatype
5. Date and time Datatypes
Capabilities of SQL SELECT Statements
Projection Selection
Table 1 Table 1
Join
Table 1 Table 2
Basic SELECT Statement
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table;
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
Using Arithmetic Operators
SELECT last_name, salary, salary + 300
FROM employees;
…
Operator Precedence
_
* / +
…
Using Parentheses
SELECT last_name, salary, 12*(salary+100)
FROM employees;
…
Defining a Null Value
• A null is a value that is unavailable, unassigned, unknown,
or inapplicable.
• A null is not the same as zero or a blank space.
SELECT last_name, job_id, salary, commission_pct
FROM employees;
…
Null Values
in Arithmetic Expressions
Arithmetic expressions containing a null value
evaluate to null.
SELECT last_name, 12*salary*commission_pct
FROM employees;
…
Defining a Column Alias
A column alias:
• Renames a column heading
• Is useful with calculations
• Immediately follows the column name - there can also be
the optional AS keyword between the column name and
alias
• Requires double quotation marks if it contains spaces or
special characters or is case sensitive
Using Column Aliases
SELECT last_name AS NAME, commission_pct comm
FROM employees;
…
Concatenation Operator
A concatenation operator:
• Concatenates columns or character strings to other
columns
• Is represented by two vertical bars (||)
• Creates a resultant column that is a character expression
Using the Concatenation Operator
SELECT last_name||job_id AS "Employees"
FROM employees;
…
Literal Character Strings
• A literal is a character, a number, or a date included in the
SELECT list.
• Date and character literal values must be enclosed within
single quotation marks.
• Each character string is output once for each
row returned.
Using Literal Character Strings
…
Duplicate Rows
The default display of queries is all rows, including
duplicate rows.
SELECT department_id
FROM employees;
…
Eliminating Duplicate Rows
Eliminate duplicate rows by using the DISTINCT
keyword in the SELECT clause.
SELECT DISTINCT department_id
FROM employees;
Displaying Table Structure
Use the iSQL*Plus DESCRIBE command to display
the structure of a table.
DESC[RIBE] tablename
Displaying Table Structure
DESCRIBE employees
Summary
• Write a SELECT statement that:
– Returns all rows and columns from a table
– Returns specified columns from a table
– Uses column aliases to give descriptive column
headings
“retrieve all
employees
in department 90”
Limiting the Rows Selected
• Restrict the rows returned by using the WHERE clause.
= Equal to
Less than
<
Less than or equal to
<=
Not equal to
<>
Using Comparison Conditions
SELECT first_name
FROM employees
WHERE first_name LIKE 'S%';
Using the LIKE Condition
• You can combine pattern-matching characters.
SELECT last_name
FROM employees
WHERE last_name LIKE '_o%';
• You can use the ESCAPE identifier to search for the actual
% and _ symbols.
Using the NULL Conditions
Test for nulls with the IS NULL operator.
•COUNT function is used to Count the number of rows in a database table. It can
work on both numeric and non-numeric data types.
•COUNT function uses the COUNT(*) that returns the count of all the rows in a
specified table. COUNT(*) considers duplicate and Null.
Syntax
1.COUNT(*)
2.or
3.COUNT( [ALL|DISTINCT] expression )
2. SUM Function
3. AVG function
4. MAX Function
5. MIN Function
1. GROUP BY
• SQL GROUP BY statement is used to arrange identical data into groups. The
GROUP BY statement is used with the SQL SELECT statement.
• The GROUP BY statement follows the WHERE clause in a SELECT statement and
precedes the ORDER BY clause.
• The GROUP BY statement is used with aggregation function.
Sample table: PRODUCT_MAST
2. HAVING
…
Sorting in Descending Order
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY hire_date DESC ;
…
Sorting by Column Alias
SELECT employee_id, last_name, salary*12 annsal
FROM employees
ORDER BY annsal;
…
Summary
In this lesson, you should have learned how to:
• Use the WHERE clause to restrict rows of output
– Use the comparison conditions
– Use the BETWEEN, IN, LIKE, and NULL conditions
– Apply the logical AND, OR, and NOT operators
• Use the ORDER BY clause to sort rows of output