0% found this document useful (0 votes)
2 views

sql_HTI_4

The document provides an overview of SQL SELECT statements, including the use of column aliases, concatenation operators, and literal character strings. It also covers the use of the WHERE clause for filtering results, comparison operators, and the DESCRIBE command for displaying table structures. Additionally, it includes examples and explanations of various SQL concepts relevant to querying data from a database.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

sql_HTI_4

The document provides an overview of SQL SELECT statements, including the use of column aliases, concatenation operators, and literal character strings. It also covers the use of the WHERE clause for filtering results, comparison operators, and the DESCRIBE command for displaying table structures. Additionally, it includes examples and explanations of various SQL concepts relevant to querying data from a database.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Using Column Aliases

SELECT last_name AS name, commission_pct AS comm


FROM employees;


SELECT last_name "Name" , salary*12 "Annual Salary"
FROM employees;

1 - 18 Copyright © 2009, Oracle. All rights reserved.


Lesson Agenda

• Basic SELECT Statement


• Arithmetic Expressions and NULL values in SELECT
statement
• Column Aliases
• Use of concatenation operator, literal character strings,
alternative quote operator, and the DISTINCT keyword
• DESCRIBE command

1 - 19 Copyright © 2009, Oracle. All rights reserved.


Concatenation Operator

A concatenation operator:
• Links columns or character strings to other columns
• Is represented by two vertical bars (||)
• Creates a resultant column that is a character expression
SELECT last_name||job_id "Employees"
FROM employees;

1 - 20 Copyright © 2009, Oracle. All rights reserved.


Literal Character Strings

• A literal is a character, a number, or a date that is included


in the SELECT statement.
• Date and character literal values must be enclosed within
single quotation marks.
• Each character string is output once for each row returned.

1 - 21 Copyright © 2009, Oracle. All rights reserved.


Using Literal Character Strings

SELECT last_name ||' is a '||job_id


"Employee Details"
FROM employees;

1 - 22 Copyright © 2009, Oracle. All rights reserved.


Alternative Quote (q) Operator

• Specify your own quotation mark delimiter.


• Select any delimiter.
• Increase readability and usability.

q'[ Department's Manager Id: ]'


SELECT department_name||manager_id "Department and Manager"

FROM departments;

1 - 23 Copyright © 2009, Oracle. All rights reserved.


Duplicate Rows

The default display of queries is all rows, including duplicate


rows.
SELECT department_id
FROM employees; 1


SELECT DISTINCT department_id
FROM employees; 2

1 - 24 Copyright © 2009, Oracle. All rights reserved.


Lesson Agenda

• Basic SELECT statement


• Arithmetic expressions and NULL values in the SELECT
statement
• Column aliases
• Use of concatenation operator, literal character strings,
alternative quote operator, and the DISTINCT keyword
• DESCRIBE command

1 - 25 Copyright © 2009, Oracle. All rights reserved.


Displaying the Table Structure

• Use the DESCRIBE command to display the structure of a


table.
• Or, select the table in the Connections tree and use the
Columns tab to view the table structure.
DESC[RIBE] tablename

1 - 26 Copyright © 2009, Oracle. All rights reserved.


Using the DESCRIBE Command

DESCRIBE employees;

1 - 27 Copyright © 2009, Oracle. All rights reserved.


Quiz

Identify the SELECT statements that execute successfully.

SELECT first_name, last_name, job_id, salary*12


1. AS Yearly_Sal
FROM employees;

SELECT first_name, last_name, job_id, salary*12


2. yearly_sal
FROM employees;

SELECT first_name, last_name, job_id, salary AS


3. yearly_sal
FROM employees;

SELECT first_name+last_name AS name, job_Id,


salary*12 yearly_sal
4.
FROM employees;

1 - 28 Copyright © 2009, Oracle. All rights reserved.


Limiting the Rows That Are Selected

• Restrict the rows that are returned by using the WHERE


clause:
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table
[WHERE condition(s)];

• The WHERE clause follows the FROM clause.

2 -5 Copyright © 2009, Oracle. All rights reserved.


Using the WHERE Clause

SELECT employee_id, last_name, job_id, department_id


FROM employees
WHERE department_id = 90 ;

2 -6 Copyright © 2009, Oracle. All rights reserved.


Character Strings and Dates

• Character strings and date values are enclosed with single


quotation marks.
• Character values are case-sensitive and date values are
format-sensitive.
• The default date display format is DD-MON-RR.

SELECT last_name, job_id, department_id


FROM employees
WHERE last_name = 'Whalen' ;

SELECT last_name FROM employees

WHERE hire_date = '17-FEB-96' ;

2 -7 Copyright © 2009, Oracle. All rights reserved.


Comparison Operators

Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
BETWEEN Between two values (inclusive)
...AND...

IN(set) Match any of a list of values

LIKE Match a character pattern

IS NULL Is a null value

2 -8 Copyright © 2009, Oracle. All rights reserved.


Using Comparison Operators

SELECT last_name, salary


FROM employees
WHERE salary <= 3000 ;

2 -9 Copyright © 2009, Oracle. All rights reserved.


Range Conditions Using the BETWEEN Operator

Use the BETWEEN operator to display rows based on a range of


values:

SELECT last_name, salary


FROM employees
WHERE salary BETWEEN 2500 AND 3500 ;

Lower limit Upper limit

2 - 10 Copyright © 2009, Oracle. All rights reserved.


Membership Condition Using the IN Operator

Use the IN operator to test for values in a list:

SELECT employee_id, last_name, salary, manager_id


FROM employees
WHERE manager_id IN (100, 101, 201) ;

2 - 11 Copyright © 2009, Oracle. All rights reserved.

You might also like