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

23
Writing Basic Select Statements

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

Page 1: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

Writing Basic Select Statements

Page 2: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

Objectives

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

List the capabilities of MySQL SELECT statementsExecute a basic SELECT statement

Page 3: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

Capabilities of MySQL SELECT Statements

Selection Projection

Table 1 Table 2

Table 1 Table 1Join

Page 4: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

Basic SELECT Statement

SELECT identifies what columns.FROM identifies which table.

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

Page 5: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

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.

Page 6: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

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;

Page 7: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

Selecting Specific Columns

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

MySQL>SELECT dept_nbr, location ->FROM department;

Page 8: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

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

Page 9: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

Arithmetic Expressions

Create expressions on numeric data by using arithmetic operators.

Operator Description+ Add- Subtract* Multiply/ Divide

Page 10: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

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.

Page 11: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

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.

* / + _

Page 12: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

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.

Page 13: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

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.

Page 14: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

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;

Page 15: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

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

Page 16: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

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

Page 17: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

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

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

...

Page 18: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

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

Page 19: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

Using the Concatenation Operator

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

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

Page 20: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

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

Page 21: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

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;

Page 22: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

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.

Page 23: After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.

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.