After completing this lesson, you should be able to do the following: List the capabilities of MySQL...

Post on 02-Jan-2016

214 views 1 download

Tags:

Transcript of After completing this lesson, you should be able to do the following: List the capabilities of MySQL...

Writing Basic Select Statements

Objectives

After completing this lesson, you should be able to do the following:

List the capabilities of MySQL SELECT statementsExecute a basic SELECT statement

Capabilities of MySQL SELECT Statements

Selection Projection

Table 1 Table 2

Table 1 Table 1Join

Basic SELECT Statement

SELECT identifies what columns.FROM identifies which table.

SELECT [DISTINCT] {*, column [alias],...}FROM table;

Writing MySQL StatementsMySQL statements are not case sensitive. MySQL statements can be on one ormore lines.Keywords cannot be abbreviated or split across lines.Clauses are usually placed on separate lines.Tabs and indents are used to enhance readability.

Selecting All Columns

dept_nbr dept_name location--------- -------------- ------------- 10 Accounting New York 20 Research Dallas 30 Sales Chicago 40 Operations Boston

MySQL>SELECT * ->FROM department;

Selecting Specific Columns

dept_nbr location--------- ------------- 10 New York 20 Dallas 30 Chicago 40 Boston

MySQL>SELECT dept_nbr, location ->FROM department;

Column Heading Defaults

Default justificationLeft: Date and character dataRight: Numeric data

Default displayCase Matches Column Name at time of creation, i.e.: hire_date vs. HIRE_DATE

Arithmetic Expressions

Create expressions on numeric data by using arithmetic operators.

Operator Description+ Add- Subtract* Multiply/ Divide

Using Arithmetic Operators

MySQL>SELECT name, salary, salary+300 ->FROM employee;

name salary salary+300---------- --------- ---------King 5000 5300Blake 2850 3150Clark 2450 2750Jones 2975 3275Martin 1250 1550Allen 1600 1900...14 rows selected.

Operator Precedence

Multiplication and division take priority over addition and subtraction.Operators of the same priority are evaluated from left to right.Parentheses are used to force prioritized evaluation and to clarify statements.

* / + _

Operator Precedence

MySQL>SELECT name, salary, 12*salary+100 ->FROM employee;

name salary 12*salary+100---------- --------- -------------King 5000 60100Blake 2850 34300Clark 2450 29500Jones 2975 35800Martin 1250 15100Allen 1600 19300...14 rows selected.

Using Parentheses

MySQL>SELECT name, salary, 12*(salary+100) ->FROM employee;

name salary 12*(salary+100)---------- --------- --------------King 5000 61200Blake 2850 35400Clark 2450 30600Jones 2975 36900Martin 1250 16200...14 rows selected.

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.

name job salary commission---------- --------- --------- ---------King President 5000Blake Manager 2850...Turner Salesman 1500 0...14 rows selected.

MySQL>SELECT name, job, salary, commission ->FROM employee;

Null Values in Arithmetic Expressions

Arithmetic expressions containing a null value evaluate to null.

MySQL>select name, 12*salary+commission ->from employee ->WHERE name= ‘King’;

name 12*salary+commission ------- --------------------King

Defining a Column Alias

Renames a column headingIs useful with calculationsImmediately follows column name; optional AS keyword between column name and aliasRequires double quotation marks if it contains spaces or special characters or is case sensitive

Using Column Aliases

MySQL>SELECT name as Employee_name, salary as Monthly_salary

->FROM employee;

Employee_name Monthly_salary

------------- --------------

...

MySQL>SELECT name "Name", -> salary*12 "Annual Salary" ->FROM employee;

Name Annual Salary

------------- -------------

...

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

MySQL>SELECT name||job AS "Employees" ->FROM employee;

Employees-------------------KingPresidentBlakeManagerClarkManagerJonesManagerMartinSalesmanAllenSalesman...14 rows selected.

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.CONCAT() function may also be used.

Discussed with other Single Row Functions

Using Literal Character Strings

Employee Details-------------------------King is a PresidentBlake is a ManagerClark is a ManagerJones is a ManagerMartin is a Salesman...14 rows selected.

MySQL>SELECT name ||' is a '||job -> AS "Employee Details" ->FROM employee;

Duplicate Rows

The default display of queries is all rows, including duplicate rows.

MySQL>SELECT dept_nbr ->FROM employee;

dept_nbr--------- 10 30 10 20...14 rows selected.

Eliminating Duplicate Rows

MySQL>SELECT DISTINCT dept_nbr ->FROM employee;

dept_nbr--------- 10 20 30

Eliminate duplicate rows by using the DISTINCT keyword in the SELECT clause.