2. Objectives
After completing this lesson, you should be able to do the following:
List the capabilities of SQL SELECT statements Execute a basic SELECT
statement this include:
Using Arithmetic Expressions
Selecting All OR Specific Column(s).
Defining a Null Value.
Defining a Column Alias
Eliminating Duplicate Rows
3. What is Structured Query Language?
SQL stands for Structured Query Language.
It is the language that is used to extract, manipulate,
and structure data that resides in a Relational
Database Management System (RDBMS). In other
words, to get an answer from your database, you must
ask the question in SQL.
4. Model Microsoft SQL Server
Microsoft SQL Server is a relational database language
based on the SQL .
Microsoft SQL Server contains two kinds of statements:
Data Definition Language (DDL) statements. These
statements let you define and modify the schema of your
database.
Data Manipulation Language (DML) statements. These
statements let you create, access and modify data in your
database.
5. Data Definition Language (DDL)
Statement Description
CREATE TABLE Defines a table in a database.
DROP TABLE Deletes a table from a database.
ALTER TABLE Modifies a table in a database.
CREATE INDEX Adds an index to a table.
DROP INDEX Deletes an index from a table.
6. Data Manipulation Language (DML)
Statement Description
SELECT
Retrieves and displays one or more rows from a
table.
INSERT INTO Adds new rows to a table.
UPDATE Changes column values in one or more rows of a
table.
DELETE FROM Deletes one or more rows from a table.
UNION
Combines two or more SELECT statements to
create
a complex query.
7. Table
In a relational database, all data is organized into tables.
Tables are the database structure that contains the
physical data, and they are organized by their columns
(or fields) and rows (or records).
8. Creating and Working with tables
Creating employees table and establish primary key
constraints. In SQL Server, tables can be created
graphically using SQL Server Management Studio
(SSMS) or using a query(Learned Later).
9. Capabilities of SQL SELECT statements
A SELECT statement retrieves information from the database. Using a
SELECT statement as the following:
1. Projection: choose (few or as many) the columns in a table
to returned by a query.
2. Selection: choose the rows in a table to returned by a query.
Various criteria used to restrict the rows.
3. Joining: it brings together data that is stored in different
tables by creating a link between them (learned later).
10. Basic SELECT Statement
1- Selecting All Columns
SELECT * FROM employees
An asterisk (*) displays all columns of the table
Or all table fields can be written.
SELECT Employe_ID, First_Name, Last_Name,
Phone_Number, Address, Salary, Hier_Date,
Department_Name FROM employees
It Selects All Columns of All Rows
11. 2-Selecting Specific Columns
• SELECT Employe_ID, Last_Name FROM employees
• It Selects Specific Columns of All Rows
• Columns order can be specified in which you want them to appear
in the output, Such that:
12. Arithmetic Expressions
Multiplication and division are evaluated first.
Same priority Operators are evaluated from left to right.
Parentheses are used to force prioritized evaluation and to clarify
statements.
13. SELECT Last_Name, Salary, Salary +300 From
employees
The calculated column (SALARY+300) is not a new
column in the EMPLOYEES table.
By default, the name of a new column comes from the
generation of ( salary+300).
Using Arithmetic Expressions
14. Operator Precedence
SELECT last_name,salary, 12*salary+100
From employees
The (*) performed before (+).
SELECT last_name,salary, (12*salary)+100
From employees
SELECT last_name,salary, 12*(salary+100)
From employees
First to force the expression. Second override the rules of
precedence
15. Defining a Null Value
If a row lacks the data value for a particular column, that value is said
to be NULL, or to contain a NULL.
A NULL is a value that is unavailable, unassigned, unknown, or
inapplicable.
Zero is a number, and a space is a character.
A NULL is not the same as zero or a blank space.
SELECT last_name, Salary, salary, 12*salary FROM employees
16. Null Values - in Arithmetic Expressions
If any column value in an arithmetic expression is
NULL, the result is NULL.
Performing division with zero, it gets an error.
Dividing a number by null, the result is a null or
unknown.
SELECT last_name, 12*salary*commission_pct
FROM employees
17. Defining a Column Alias
A column Heading Alias:
Renames a column heading, is useful with calculations.
Immediately follows the column name.
Optional AS keyword used between the column and
alias.
Requires double quotation marks if it contains spaces or
special characters (# or $).
It can be used in both the SELECT and the ORDER BY
clause. It cannot be used in the WHERE clause.
18. Using Column Aliases
SELECT last_name AS name , Department_Name Dept
FROM employees
SELECT last_name "Name ", salary*12 "Annual Salary"
FROM employees
19. Eliminating Duplicate Rows
The default display of queries is all rows, including duplicate
rows.
SELECT department_NameFROM employees
Eliminate duplicate rows by using the DISTINCT keyword
in the SELECT clause.
SELECT DISTINCT department_Name FROM employees