Writing Basic SQL SELECT Statements

Slides:



Advertisements
Similar presentations
Restricting and sorting data 16 May May May Created By Pantharee Sawasdimongkol.
Advertisements

1Eyad Alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort the rows retrieved by a query.
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 Oracle9i: SQL1 Basic SQL SELECT Statements.
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.
Chapter 1 Writing Basic SQL Statements Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina,
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
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.
SQL (DDL & DML Commands)
2 Writing Basic SELECT Statements. 1-2 Copyright  Oracle Corporation, All rights reserved. Capabilities of SQL SELECT Statements Selection Projection.
Copyright  Oracle Corporation, All rights reserved. Writing Basic SQL Statements.
RELATSIOONILISED ANDMEBAASID(alg) SQLi VÕIMALUSED.
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 [ ],...]
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
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.
2-1 Limiting Rows Using a Selection “…retrieve all employees in department 10” EMP EMPNO ENAME JOB... DEPTNO 7839KINGPRESIDENT BLAKEMANAGER CLARKMANAGER.
1 Chapter 2 Basic SQL SELECT Statements. 2 Chapter Objectives Distinguish between an RDBMS and an ORDBMS Identify keywords, mandatory clauses, and optional.
1-1 Copyright  Oracle Corporation, All rights reserved. Logging In to SQL*Plus From Windows environment:From Windows environment: From command line:From.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
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.
Communicating with a RDBMS Using SQL Database SQL> SELECT loc 2 FROM dept; SQL> SELECT loc 2 FROM dept; SQL statement is entered Statement is sent to database.
Restricting and Sorting Data
Retrieving Data Using the SQL SELECT Statement
Basic CRUD in SQL Server
Basic select statement
Restricting and Sorting Data
Restricting and Sorting Data
Writing Basic SQL SELECT Statements
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
Retrieving Data Using the SQL SELECT Statement
Restricting and Sorting Data
Restricting and Sorting Data
Lab 2: Retrieving Data from the Database
Presentation transcript:

Writing Basic SQL SELECT Statements

sql SQL stands for Structured Query Language SQL lets you access and manipulate databases

Types of SQL Statements

Writing SQL Statements SQL statements are not case sensitive. SQL statements can be on one or more lines. Keywords cannot be abbreviated or split across lines. Clauses are usually placed on separate lines.

Basic SELECT Statement Select { *, column_name} from Table_name; SELECT identifies what columns FROM idenifies which table

In the syntax: KEYWORD MEANING SELECT is a list of one or more columns * selects all columns Column-expression selects the named column or the expression FROM table specifies the table containing the columns

Selecting All Columns

Selecting Specific Columns

Arithmetic Expressions Create expressions with number and date data by using arithmetic operators.

Using Arithmetic Operators Note: Resultant calculated column vacationHours+10 is not a new column in the humanResources.Employee table; it is for display only. By default, there is no name for the new column

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

Using Parentheses

Defining a Column Alias Renames a column heading Is useful with calculations Immediately follows the column name: there can also be the optional AS keyword between the column name and alias Requires double quotation marks if it contains spaces or special characters or is case sensitive

Using Column Aliases

Concatenation Operator A concatenation operator: Concatenates columns or character strings to other columns It uses the function of CONCAT();  It requires a minimum of two input values; otherwise, an error is raised.

Using the Concatenation Operator

Literal Character Strings A literal value 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

Using Literal Character Strings

Using Literal Character Strings

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

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

Using Where Clause

Restrict the rows returned by using the WHERE clause The WHERE clause follows the FROM clause In this syntax: KEYWORD MEANING WHERE restricts the query to rows that meet a condition condition is composed of column names, a comparison operator and constant |list of values|column names

Character Strings and Dates Character strings and dates in the WHERE clause must be enclosed in single quotation marks (''). Number or constants, however should not be enclosed in single quotation marks.

Comparison Conditions OPERATOR MEANING = Equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to <> Not equal to

Comparison Conditions Comparison conditions are used in conditions that compare one expression to another value or expression. They are used in the WHERE clause in the following format: ... WHERE expr operator value Example ... WHERE hire_date='01-JAN-95' ... WHERE salary>=6000 ... WHERE last_name='Smith‘ An alias cannot be used in the WHERE clause.

Using comparison Condition

Other Comparison Conditions OPERATOR MEANING BETWEEN ...AND... Between two values (inclusive) IN(set) Match any of a list of values LIKE Match a character pattern IS NULL Is a null value

Using the BETWEEN Condition Use the BETWEEN condition to display rows based on a range of values. Values specified with the BETWEEN condition are inclusive. You must specify the lower limit first.

Using the IN Condition Use the IN membership condition to test for values in a list.

Using the IN Condition(Contd..) The IN condition can be used with any data type. The following example returns a row from the EMPLOYEES table for any employee whose last name is included in the list of names in the WHERE clause: If characters or dates are used in the list, they must be enclosed in single quotation marks ('').

Using the LIKE Condition Use the LIKE condition to perform character pattern-matching operation Search conditions can contain either literal characters or numbers: – % denotes zero or many characters. – _ denotes one character.

Use like Condition

Using the LIKE Condition You can combine pattern-matching characters.

Using the NULL Conditions Test for nulls with the IS NULL operator.

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 Three logical operators are available in SQL: • AND • OR • NOT

Logical Conditions(Contd..) OPERATOR MEANING AND Returns TRUE if both component conditions are true OR Returns TRUE if either component condition is true NOT Returns TRUE if the following condition is false You can use several conditions in one WHERE clause using the AND and OR operators.

Using the AND Operator AND requires both conditions to be true.

Using the OR Operator OR requires either condition to be true.

Using Not operator in select

Rules of Precedence

ORDER BY Clause Sort rows with the ORDER BY clause – ASC: ascending order (the default order) – DESC: descending order The ORDER BY clause comes last in the SELECT statement. A 1 Z 9 Z 9 A 1

Order by clause

Sorting in Descending order

Sorting by Column Alias

Sorting by Column Position in SELECT List 1 2 3 4 SELECT employee_id, first_name, last_name, department_id FROM employees ORDER BY 2 ; … …

Sorting by Multiple Columns The order of ORDER BY list is the order of sort.

exercise 1. Create a query to display the job title and vacation hours of the employee whose hours are greater than 50. (hint: humanresources.employee table) 2. Create a query to display the Gender, birthday and job title for employee whose date of birth is 1969-1-29 3. Create a query to display the sales orderID and unit price for all the sales which are not under the range of 1500 and 2850 (hint: sales.salesorderdetail table)

exercise 4. Display the employee job title, Gender and hire date of employees hired between February 20, 2007 and May 1, 2008. order the query in ascending order by hire date. 5. Display the currency code of following countries Pakistan Rupees, saudi Riyal and singapore Dollar. (hint: use sales.currency table) 6. List the job title, hire date and vacation hours of the employee whose vacation hours are greater than 30. lable the column as job description, joining date and unpaid hours respectively (hint: use humanresource.employee table)

Exercise 7. Display the first, middle and last name of the employees whose name starts with ‘D’ (Hint: use person.person table) 8. Display the first name of the employee whose middle name is not null. 9. Display the department id, name and groupname and sort the data in the descending order by groupname. 10. Display the names of all employees where third letter of their name is an A

Exercise 11. Display the names of all the employees that have two Ls in their name and have title of ‘Mr.’ 12. Display the job title, nationalIDnumber, hire date for all the employees whose job title is marketing assistant, accounts manager, recruiter and their vacation hours are not equal to 10,15 and 50. 13. Display the job title, vacation hours, vacation hrs in days in a separate column named vacations in days

The End