Basic select statement

Slides:



Advertisements
Similar presentations
1Eyad Alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Advertisements

LECTURE 8.  Consider the table employee(employee_id,last_name,job_id, department_id )  assume that you want to display all the employees in department.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
Introduction to Structured Query Language (SQL)
Restricting and Sorting Data. Consider the table employee(employee_id,last_name,job_id, department_id ) assume that you want to display all the employees.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Writing Basic SQL statement 2 July July July Create By Pantharee Sawasdimongkol.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
Ceng 356-Lab1. Objectives After completing this lesson, you should be able to do the following: Get Familiar with the development environment List the.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Chapter 2 Basic SQL SELECT Statements
Chapter 2 Basic SQL SELECT Statements Oracle 10g: SQL.
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
1 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Restricting and Sorting Data. ◦ Limiting rows with:  The WHERE clause  The comparison conditions using =,
2 Copyright © Oracle Corporation, All rights reserved. Restricting and Sorting Data.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
4 Copyright © 2006, Oracle. All rights reserved. Restricting and Sorting Data.
LECTURE 8.  Consider the table employee(employee_id,last_name,job_id, department_id )  assume that you want to display all the employees in department.
After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.
2 Writing Basic SELECT Statements. 1-2 Copyright  Oracle Corporation, All rights reserved. Capabilities of SQL SELECT Statements Selection Projection.
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
1 Writing Basic SQL Statements. 1-2 Objectives At the end of this lesson, you should be able to: List the capabilities of SQL SELECT statements Execute.
2 第二讲 Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query.
Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement Satrio Agung Wicaksono, S.Kom., M.Kom.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
Retrieving Data Using the SQL SELECT Statement. Objectives After completing this lesson, you should be able to do the following: – List the capabilities.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
Working with Columns, Characters, and Rows. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Apply the concatenation.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
1 Chapter 2 Basic SQL SELECT Statements. 2 Chapter Objectives Distinguish between an RDBMS and an ORDBMS Identify keywords, mandatory clauses, and optional.
9/29/2005From Introduction to Oracle:SQL and PL/SQL, Oracle 1 Restricting and Sorting Data Kroenke, Chapter Two.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following: –List the capabilities of SQL SELECT statements.
Limiting Selected Rows. 2-2 Objectives Sort row output using the ORDER BY clause. Sort row output using the ORDER BY clause. Enter search criteria using.
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Copyright س Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
1 Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Restricting and Sorting Data
RELATSIOONILISED ANDMEBAASID (algus , ORACLE materjalid)
Retrieving Data Using the SQL SELECT Statement
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT Statements
ATS Application Programming: Java Programming
Restricting and Sorting Data
Retrieving Data Using the SQL SELECT Statement
Using the Set Operators
Restricting and Sorting Data
Writing Basic SQL SELECT Statements
Using the Set Operators
Retrieving Data Using the SQL SELECT Statement
Restricting and Sorting Data
Writing Basic SQL SELECT Statements
Restricting and Sorting Data
Writing Basic SQL SELECT Statements
Writing Basic SQL Statements
Retrieving Data Using the SQL SELECT Statement
Restricting and Sorting Data
Using the Set Operators
Restricting and Sorting Data
Presentation transcript:

Basic select statement LECTURE FIVE Basic select statement Basic SELECT Statements

Basic SELECT statement The simplest form of the SELECT statement must include; The SELECT clause; this specifies the columns that are to be retrieved from a table/relation The FROM clause; this specifies the table from which the specified columns are to be retrieved. Basic SELECT Statements

Basic SELECT Statements General syntax The generalised syntax of a basic select statement is as follows: SELECT expressions_and_columnList FROM table_name [WHERE some condition(s) is true] [GROUP BY columnList] [HAVING condition] [ORDER BY columnList [ASC | DESC]] ; Basic SELECT Statements

SELECT Statement Cont’d When writing the select statement, the following should be at the back of ones mind; SQL statements can be written on one or more lines SQL statements are not case sensitive, unless indicated; keywords are usually written in uppercase and other words in lowercase. New lines and indents can be used to readability purposes e.g. clauses are usually written on separate lines for readability purposes. Keywords can not be split or abbreviated. Basic SELECT Statements

SELECT Statement Cont’d Notes: SELECT and FROM clauses are mandatory The order of execution is FROM table(s) [WHERE condition] [GROUP BY group_by_expression] [HAVING group_condition] SELECT column_list [ORDER BY column_list] Basic SELECT Statements

Capabilities of a SELECT Statement Join – combines information from two or more tables. Projection – specifying columns to be displayed (achieved through the use of the SELECT clause). Selection – specifying rows to be retrieved (achieved through the use of the WHERE clause). Basic SELECT Statements

Confirming Relational Schemas in an Oracle Relational DB To view existing relations (tables): Select * from user_tables; N.B.: The purpose of each table is to store data that describes exactly one entity - a thing of significance about which information needs to be known. To view a particular relation schema: Describe employees; N.B.: Identify the various attribute data types. Basic SELECT Statements

Data Retrieval from one Table Four major scenarios of data retrieval from a single table: Retrieving all columns and all rows; Retrieving specific columns and all rows; Retrieving all columns and specific rows; Retrieving specific columns and specific rows. Basic SELECT Statements

(i) Retrieving all columns and all rows from the employees table. SELECT * FROM employees;   Alternatively specify all columns of the employees table; separate columns with commas, order of columns does not matter. Basic SELECT Statements

Basic SELECT Statements (ii) Retrieving specific columns and all rows from the employees table. SELECT employee_id, job_id, last_name FROM Employees; Basic SELECT Statements

Basic SELECT Statements (iii) Retrieving all columns and specific rows from the employees table. SELECT * FROM Employees WHERE job_id = 'IT_PROG'; Basic SELECT Statements

Basic SELECT Statements (iv) Retrieving specific columns and specific rows from the employees table. SELECT employee_id, job_id, last_name FROM Employees WHERE Job_id = 'IT_PROG'; Basic SELECT Statements

Basic SELECT Statements SELECTION This is the SQL capability of specifying the rows one is interested in retrieving from a table (Row selection) This is done using the WHERE clause. General syntax: SELECT clause FROM table WHERE condition; Comparison operators are used to define conditions in the where clause. Basic SELECT Statements

Basic SELECT Statements Row Selection Cont’d Note Character strings and dates in the where clause must be enclosed in single quotation marks (‘ ‘). Numeric constants should not be enclosed in single quotation marks. All character searches are case sensitive. Basic SELECT Statements

Basic SELECT Statements Row Selection Cont’d Note Character strings and dates in the where clause must be enclosed in single quotation marks (‘ ‘). Numeric constants should not be enclosed in single quotation marks. All character searches are case sensitive. Basic SELECT Statements

Basic SELECT Statements Row Selection Cont’d Logical Conditions A logical condition combines the result of two component conditions to produce a single result based on them or inverts the result of a single condition. A row is returned only if the overall result of the condition is true. The three logical operators available in SQL are: NOT AND OR Basic SELECT Statements

Basic SELECT Statements Quiz Describe the intension of the following SQL queries: SELECT first_name,last_name, job_id, department_id FROM employees WHERE first_name >'Susan'; SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary >= 2500 and job-id like '%MAN%'; SELECT last_name, job-id FROM employees WHERE job-id not in ('AC_ACCOUNT', 'FI_ACCOUNT'); Basic SELECT Statements

Using the DISTINCT Keyword Purpose – enables the retrieval of a unique set of tuple(s) or row(s). SELECT DISTINCT department_id FROM employees; You can specify multiple columns after the DISTINCT qualifier which results into distinct tuples. SELECT DISTINCT department_id, job_id Basic SELECT Statements

Arithmetic operators and Expressions The Arithmetic operators present in SQL include: +, -, *, / These can be used in any clause except the FROM clause; Same operator precedence rules that apply in SQL statements Arithmetic expression can contain column names, constant values and the arithmetic operator. Basic SELECT Statements

Using Expressions in a SELECT Clause Date expressions (+, –) SELECT last_name, hire_date, hire_date + 3 FROM employees WHERE job_id='ST_MAN'; Numeric expressions (+, –, * , / ) SELECT last_name, salary, salary + 300 FROM employees; Character expressions ( || ) SELECT first_name || last_name N.B: An expression that involves a null value returns a null value. Basic SELECT Statements

Basic SELECT Statements Using Column Aliases A column heading is changed by using a column aliase with the purpose of making the column heading more descriptive. Double quotation marks must be used when the aliase has spaces, is case sensitive or has special characters. Is useful for calculations May be defined using the AS keyword. SELECT first_name || ' ' || last_name "Name", salary*12 "Annual Salary" FROM employees; Basic SELECT Statements

Literal character strings A literal character is a character, number or date included in a SELECT list A data and character literal values must be enclosed in single quotation marks The literal character string value is output one for each row that is retrieved. Basic SELECT Statements

Using the ORDER BY clause Facilitates sorting of data results in a particular order / direction. The order by clause consists of column identifies that the result is to be sorted on, separated by commas. The column identifier can be a column name / expression / alias / position. Sorting is done in ascending (ASC) or descending (DESC) order on any combination of columns, regardless of whether that column appears in the result. Sorting is by default ascending. Basic SELECT Statements

Basic SELECT Statements ORDER BY Clause Cont’d Data Sorting Example: SELECT last_name, job_id, department_id, hire_date FROM employees WHERE job_id like '_A% ' ORDER BY hire_date; Note: The ORDER BY clause must be the last clause of the SQL statement. You can sort by multiple columns. Basic SELECT Statements