Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.

Slides:



Advertisements
Similar presentations
WHERE Clause Chapter 2. Objectives Limit rows by using a WHERE clause Use the LIKE operator Effect of NULL values Use compound conditions Use the BETWEEN.
Advertisements

Virtual training week 4 structured query language (SQL)
1Eyad Alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View 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.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
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.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
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.
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.
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.
Database Design Sections 17 & 18 – Concatenations, DISTINCT, DESCRIBE, Logical Operators, Order of Operations, Sorting.
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 [ ],...]
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.
1 SQL SQL (Structured Query Language) : is a database language that is used to create, modify and update database design and data. Good Example of DBMS’s.
Oracle 11g DATABASE DEVELOPMENT LAB2. Chapter- 2  These commands, which could be issued from SQL*Plus or SQL Developer,  will make it possible to log.
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.
Session 9 Accessing Data from a Database. RDBMS and Data Management/ Session 9/2 of 34 Session Objectives Describe the SELECT statement, its syntax and.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
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.
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.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
1 Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Restricting and Sorting Data
Retrieving Data Using the SQL SELECT Statement
Basic CRUD in SQL Server
Writing Basic SQL SELECT Statements
Basic select statement
ATS Application Programming: Java Programming
Restricting and Sorting Data
Writing Basic SQL SELECT Statements
Retrieving Data Using the SQL SELECT Statement
Restricting and Sorting Data
“Manipulating Data” Lecture 6.
“Manipulating Data” Lecture 6.
Writing Basic SQL SELECT Statements
Chapter 2 Views.
Restricting and Sorting Data
Writing Basic SQL SELECT Statements
Retrieving Data Using the SQL SELECT Statement
Restricting and Sorting Data
Restricting and Sorting Data
Presentation transcript:

Simple Queries DBS301 – Week 1

Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates Sort data by using an ORDER BY clause

Basic SELECT statement SELECT expression1, expression2, … FROM table An expression may involve one or more column(s), literal(s), and/or arithmetic operator(s), … ; eg. last_name, salary * 1.1 Example: SELECT last_name, first_name, commission_pct, commission_pct*1.2 FROM employees An * in the SELECT clause represents all columns, eg: SELECT * FROM employees;

Aliases An alias can be provided for any expression in a SELECT clause and is used as an alternate column heading Keyword AS is optional when specifying alias Alias follows expression in SELECT clause If alias is made up of more than a single word then alias must be enclosed within double quotation marks Example: SELECT last_name, first_name, commission_pct*1.2 AS “Increased Comm” FROM employees;

Literals Literals are actual values that can be used in an SQL statement May be numeric value, eg 2, 1.2 May be character strings, eg ‘Employee:’, ‘2’

Concatenation operator Concatenation operator || is used to join strings and is often used to format output Example: SELECT first_name || last_name || ‘ has commission rate of’ || commission_pct FROM employees; Normally good to use alias for expressions involving || operation for meaningful heading: SELECT first_name || last_name || ‘ has commission rate of’ || commission_pct AS “EMPLOYEE COMMISSIONS” FROM employees;

DISTINCT Duplicate values may be returned by a SELECT statement. Use of DISTINCT allows duplicate rows of output to be eliminated. Example: list the department for all employees: SELECT department_id FROM employees; Output from query contains duplicate values of department # and is not clear. Use of DISTINCT in query removes duplicate rows from output: SELECT DISTINCT department_id FROM employees;

ORDER BY Sort output by using an ORDER BY clause May sort by one or more expressions First expression is primary sort field, second expression is then used, … Example of listing employee names in alphabetical order: SELECT first_name || ‘ ’ || last_name AS NAME FROM employees ORDER BY last_name, first_name; But is this output in alphabetical order by last name and first name? Can’t tell because there are not 2 employees with the same last name!

Testing of SQL Statements SQL is a programming language SQL statements must be tested just as you must test C or C++ programs It is not okay to assume that output and/or results produced by an SQL statement you write is correct! Many important business decisions may be made based on the output produced by SQL statements that you write – therefore every statement you write must be thoroughly tested. To test SQL statements you must have a wide range of data values in your test database

Testing of SQL Statements (ctd) You should determine what results you expect based on your client’s requirements and the data contained in the database Then develop your SQL statement (often in stages) Then execute your statement Compare the output and/or results with what you predicted. If they are not the same then verify if the results you predicted are correct. If the predicted results are correct then you must find and correct a logical error in your SQL statement It is very easy to write syntactically valid but logically incorrect SQL statements

Testing of ORDER BY Example of listing employee names in alphabetical order: SELECT first_name || ‘ ’ || last_name FROM employees ORDER BY last_name, first_name; How to test if the output produced by the query is in alphabetical order by last and first name? Must add another employee with same last name as an existing employee INSERT INTO employees VALUES(15, ‘John',‘King', NULL,'12-NOV-04', 'IT_PROG', NULL, NULL, NULL, NULL); Now rerun query and verify results

ORDER BY clause (ctd) Expression(s) used in ORDER BY clause may or may not be included in SELECT clause Example: list employee names in order by department id and then for any employees located in the same department in alphabetical order by last and first name: SELECT first_name || ‘ ’ || last_name AS NAME FROM employees ORDER BY department_id, last_name, first_name;

ORDER BY clause (ctd) Data may be sorted in ascending or descending order by using the keywords ASC or DESC Default order is ascending Example: list employee names from the highest numbered employee: SELECT first_name || ‘ ’ || last_name AS NAME FROM employees ORDER BY employee_id DESC