Lab 2: Retrieving Data from the Database

Slides:



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

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.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.
SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition.
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.
Lab 5: Subqueries CISB224 02A, 02B Semester I, 2009/2010 College of Information Technology, Universiti Tenaga Nasional 1.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Rationale Aspiring Database Developers should be able to efficiently query and maintain databases. This module will help students learn the Structured.
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.
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
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.
Structured Query Language
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
ORDER BY clause in SELECT command: Normally, the result of the query will not be in ordered format. If we want to get the result of the query in specific.
Lab 3: Single-row Functions College of Information Technology, Universiti Tenaga Nasional 1 CISB224 02A, 02B Semester I, 2009/2010.
College of Information Technology, Universiti Tenaga Nasional1 Lab 2: Single-row Functions CISB224 01A CCSB244 01A Semester I, 2008/2009.
9/29/2005From Introduction to Oracle:SQL and PL/SQL, Oracle 1 Restricting and Sorting Data Kroenke, Chapter Two.
Rules of Precedence The rules of precedence determine the order in which expressions are evaluated and calculated. The next table lists the default order.
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.
Writing Basic SQL SELECT Statements Lecture
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.
1 Section 3 - Select Statement u The Select statement allows you to... –Display Data –Specify Selection Criteria –Sort Data –Group Data for reporting –Use.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Restricting and Sorting Data
Chapter 3 Introduction to SQL(2)
Connect to SQL Server and run select statements
Writing Basic SQL SELECT Statements
Basic select statement
ATS Application Programming: Java Programming
Using the Set Operators
Restricting and Sorting Data
Writing Basic SQL SELECT Statements
Using the Set Operators
Restricting and Sorting Data
Lab 5: Subqueries CISB224 02A, 02B Semester I, 2009/2010
Writing Basic SQL SELECT Statements
Restricting and Sorting Data
Writing Basic SQL SELECT Statements
Section 4 - Sorting/Functions
Lab 4: Displaying Data from Multiple Tables
Restricting and Sorting Data
Using the Set Operators
Lab 3: Single-row Functions
‘ORDER BY’ Order by clause allows sorting of query results by one or more columns. Sorting can be done in ascending or descending. Default order is ascending.
Restricting and Sorting Data
Presentation transcript:

Lab 2: Retrieving Data from the Database CISB224 02A, 02B Semester I, 2009/2010 College of Information Technology, Universiti Tenaga Nasional

The SELECT Statement Syntax SELECT select_list FROM table_source [WHERE search_condition] [GROUP BY group_by_expression] [HAVING search_condition] [ORDER BY order_by_expression] A query must be comprised of the SELECT and FROM clauses All other clauses are optional (denoted by [ ]) College of Information Technology, Universiti Tenaga Nasional

The SELECT Clause Syntax SELECT [ALL | DISTINCT] { * | {column_name | expression} [[AS] column_alias] } [,…n] This is called the select list! College of Information Technology, Universiti Tenaga Nasional

The SELECT Clause – cont. The select list must be comprised (denoted by {}) of one or more of the following (denoted by [,…n]) * - a “shortcut” that means all columns in the table {column_name|expression} – your choice (denoted by |) of either a column from the table or an expression that evaluates to a value e.g. a string, number, date, etc. College of Information Technology, Universiti Tenaga Nasional

The SELECT Clause – cont. Example 1 SELECT Name, Title FROM emp Display the employees’ names and their job titles. Note! SQL is not case-sensitive Write clauses on separate lines to enhance readability College of Information Technology, Universiti Tenaga Nasional

The SELECT Clause – cont. A column alias is used to: Replace the heading of a table column in the result Provide a heading to a column formed from an expression Use ‘ ’ to enclose a column alias when it is made up of two or more words College of Information Technology, Universiti Tenaga Nasional

The SELECT Clause – cont. Example 2 SELECT Name Employee_Name, Title ‘Job Title’ FROM emp Display the employees’ last names with the heading Employee_Name and their job titles with the heading ‘Job Title’. College of Information Technology, Universiti Tenaga Nasional

The SELECT Clause – cont. The DISTINCT argument is used when you do not want to view duplicate rows in the result ALL is the default argument and need not be specified College of Information Technology, Universiti Tenaga Nasional

The SELECT Clause – cont. Example 3 Display the State where customers are located. SELECT DISTINCT State FROM Cust College of Information Technology, Universiti Tenaga Nasional

The SELECT Clause – cont. Arithmetical expressions in SQL: may be formed from columns of numeric type and/or constants are governed by the normal evaluation rules 2 * (3 + 4) = ? Operation Operator Addition + Subtraction - Multiplication * Division / The addition operator, +, may also be used for string concatenation College of Information Technology, Universiti Tenaga Nasional

The SELECT Clause – cont. Example 4 SELECT Name, BuyingPrice + BuyingPrice * 25 / 100 FROM Product Display the products’ names and their new price as 25% more than the current price College of Information Technology, Universiti Tenaga Nasional

The SELECT Clause – cont. Example 5 SELECT ID + ‘ ’ + Name as ‘Employee Detail’ FROM emp Display the employees’ full name as one column. College of Information Technology, Universiti Tenaga Nasional

The ORDER BY Clause Syntax [ORDER BY {order_by_expression [ASC|DESC]} [,…n]] College of Information Technology, Universiti Tenaga Nasional

The ORDER BY Clause – cont. The order_by_expression may be: A column name May or may not be in the select list A column alias Expressions other than the above The position of the expression in the select list College of Information Technology, Universiti Tenaga Nasional

The ORDER BY Clause – cont. ASC On characters - alphabetical order On numbers - increasing order On dates – chronological order DESC – opposite of the above! College of Information Technology, Universiti Tenaga Nasional

The ORDER BY Clause – cont. Example 6 SELECT StartDate, Name, ID FROM emp ORDER BY StartDate DESC Display all employees’ start date, ID and name. Sort the results by start date and name, both in descending order. College of Information Technology, Universiti Tenaga Nasional

The ORDER BY Clause – cont. Example 7 Display all employees’ start date along with ID and name as one column i.e. ‘E60002, Zarina Majid. Sort the results by start date and name, both in descending order. College of Information Technology, Universiti Tenaga Nasional

The ORDER BY Clause – cont. Example 7 – cont. SELECT StartDate, ID + ‘, ’ + Name AS Emp_Detail FROM emp ORDER BY StartDate DESC, 2 DESC ORDER BY StartDate DESC, Emp_Detail DESC ORDER BY StartDate DESC, ID + ‘, ’ + Name DESC College of Information Technology, Universiti Tenaga Nasional

The WHERE clause Syntax [WHERE column_name operator expression] This syntax is simplified for the purpose of today’s (very gentle) introduction to the WHERE clause  College of Information Technology, Universiti Tenaga Nasional

The WHERE clause – cont. Comparison Operators = Is equal to <> Is not equal to < Is less than <= Is less than or equal to > Is greater than >= Is greater than or equal to College of Information Technology, Universiti Tenaga Nasional

The WHERE clause – cont. Example 8 SELECT Name, Salary, Dept FROM emp WHERE Dept=‘Admin’ Display the names, salary and department for all employees in Admin department. College of Information Technology, Universiti Tenaga Nasional

The WHERE clause – cont. Example 9 SELECT * FROM Inventory WHERE AmountInStock < ReorderPoint Display all products whose amount in stock have fallen below the reorder point. College of Information Technology, Universiti Tenaga Nasional

The WHERE clause – cont. Logical Operators BETWEEN Between two values, inclusive ReorderPointBETWEEN 25 AND 50 IN In a list of values State IN (Johor, Perak, KL) College of Information Technology, Universiti Tenaga Nasional

The WHERE clause – cont. Example 10 SELECT Name, Salary, Dept FROM emp WHERE Salary BETWEEN 1200 AND 2000 Display the names of all employees whose salary is between 1200 and 2000 College of Information Technology, Universiti Tenaga Nasional

The WHERE clause – cont. Example 11 SELECT State FROM Cust WHERE State IN (Johor, Perak, KL) Display all suppliers who are located in Johor, Perak and KL. College of Information Technology, Universiti Tenaga Nasional