Bordoloi and Bock Chapter 3 :Single Table Query Basics.

Slides:



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

Bordoloi and Bock Chapter 5 : Aggregate Row Functions.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
Bordoloi and BockCopyright 2004 Prentice Hall, Inc.7-1 COS 346 Day 16.
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14.
Structured Query Language Part I Chapter Three CIS 218.
Pretice Hall © COS 346 Day Agenda Questions?Questions? Assignment 5 DueAssignment 5 Due Assignment 6 PostedAssignment 6 Posted –Due March.
Pretice Hall © COS 346 Day 12. Pretice Hall © Agenda Questions?Questions? Quiz 1 correctedQuiz 1 corrected –1 A, 5 B’s, 1 C and 1 F Assignment.
Microsoft Access 2010 Chapter 7 Using SQL.
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
ASP.NET Programming with C# and SQL Server First Edition
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.
Chapter 3 Single-Table Queries
Bordoloi and Bock Chapter 6 : JOINS. Bordoloi and Bock A TYPICAL JOIN OPERATION Following figure displays the employee and department tables.Following.
Introduction to databases and SQL. What is a database?  A database is an organized way of holding together pieces of information  A database refers.
Bordoloi and Bock Chapter 4 : Adding Power to Queries.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
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.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
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 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
Copyright © Curt Hill Queries in SQL More options.
Bordoloi and BockCopyright 2004 Prentice Hall, Inc.6-1 CHAPTER 6: JOINS.
Pretice Hall © Chapter 3: Single Table Query Basics SQL for SQL Server Bijoy Bordoloi and Douglas Bock.
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
1 Querying a Single Table Structured Query Language (SQL) - Part II.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
George Mpopo | Rosebank College ADVANCED DATABASES WITH ORACLE 11g FOR ADDB7311 LEARNING UNIT 2 of 7.
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.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
1 Chapter 2 Basic SQL SELECT Statements. 2 Chapter Objectives Distinguish between an RDBMS and an ORDBMS Identify keywords, mandatory clauses, and optional.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM 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 SELECT Statements Lecture
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.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
Prentice Hall © Chapter 7: Subquieres SQL for SQL Server Bijoy Bordoloi and Douglas Bock.
Other database objects (Sequence and view). What Is a Sequence? A sequence: Automatically generates sequential numbers Is a sharable object Is typically.
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Retrieving Data Using the SQL SELECT Statement
Connect to SQL Server and run select statements
Writing Basic SQL SELECT Statements
Basic select statement
ATS Application Programming: Java Programming
Chapter 8: Views and Synonyms
Chapter 4: Adding Power to Queries
Chapter 5: Aggregate Functions and Grouping of Data
Writing Basic SQL SELECT Statements
Chapter 4 Summary Query.
Writing Basic SQL SELECT Statements
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

Bordoloi and Bock Chapter 3 :Single Table Query Basics

Bordoloi and Bock SIMPLE SELECT STATEMENTS The main element in a SQL query is the SELECT statement. The main element in a SQL query is the SELECT statement. A properly written SELECT statement will always produce a result in the form of one or more rows of output. A properly written SELECT statement will always produce a result in the form of one or more rows of output. The SELECT statement chooses (selects) rows from one or more tables according to specific criteria. The SELECT statement chooses (selects) rows from one or more tables according to specific criteria.

Bordoloi and Bock Example SELECT * FROM employee; EMP_SSN EMP_LAST_NAME EMP_FIRST_NAME EMP_MIDDLE_NAME Bordoloi Bijoy Joyner Suzanne A Zhu Waiman Z more rows and columns will be displayed This query selects rows from the “employee” table. This query selects rows from the “employee” table. The asterisk (*) tells Oracle to select (display) all columns contained in the table “employee”. The asterisk (*) tells Oracle to select (display) all columns contained in the table “employee”.

Bordoloi and Bock Example The following SELECT statement produces an identical output.The following SELECT statement produces an identical output. SELECT emp_ssn, emp_last_name, emp_first_name, emp_middle_name, emp_address, emp_city, emp_state, emp_zip, emp_date_of_birth, emp_address, emp_city, emp_state, emp_zip, emp_date_of_birth, emp_salary, emp_parking_space, emp_gender, emp_salary, emp_parking_space, emp_gender, emp_dpt_number, emp_superssn emp_dpt_number, emp_superssn FROM employee;

Bordoloi and Bock Oracle will process the query regardless of whether you type the entire query on one line, or indent.Oracle will process the query regardless of whether you type the entire query on one line, or indent. There are no rules about how many words can be put on a line or where to break a line.There are no rules about how many words can be put on a line or where to break a line. Although Oracle does not require indenting, indenting enhances readability.Although Oracle does not require indenting, indenting enhances readability. Indenting SQL Code

Bordoloi and Bock The following keywords are your signal to start a new line.The following keywords are your signal to start a new line. »SELECT »FROM »WHERE »GROUP BY »HAVING »ORDER BY Indenting SQL Code

Bordoloi and Bock Specify the column names to be displayed in the result set by typing the exact, complete column names.Specify the column names to be displayed in the result set by typing the exact, complete column names. Separate each column name with a comma (,).Separate each column name with a comma (,). Specify the name of the table after the FROM clause.Specify the name of the table after the FROM clause. Terminate the query with a semi-colon (;).Terminate the query with a semi-colon (;). SELECT emp_ssn, emp_last_name, emp_first_name FROM employee; Selecting Specific Columns

Bordoloi and Bock There are syntactical rules that must be followed or Oracle gives an error message instead of the desired result table.There are syntactical rules that must be followed or Oracle gives an error message instead of the desired result table. Oracle communicates errors in SELECT statements by providing unique error numbers and accompanying error descriptions.Oracle communicates errors in SELECT statements by providing unique error numbers and accompanying error descriptions. Common Errors

Bordoloi and Bock FROM Keyword Missing The next SELECT statement is missing the FROM clause so that no table name has been specified.The next SELECT statement is missing the FROM clause so that no table name has been specified. Without a table name, the database management system does not know which table to query.Without a table name, the database management system does not know which table to query. SELECT emp_ssn; ERROR at line 1: ORA-00923: FROM keyword not found where expected

Bordoloi and Bock THE DISTINCT CLAUSE Oracle provides a means for eliminating duplicate rows in a result table through use of the DISTINCT keyword.Oracle provides a means for eliminating duplicate rows in a result table through use of the DISTINCT keyword. SELECT emp_salary FROM employee; EMP_SALARY $55, $55, $43, $43, $25, $25, $30, $30, $38, $38, $25, $25, rows selected.

Bordoloi and BockDISTINCT The query is rewritten using the DISTINCT keyword to eliminate duplicate rows.The query is rewritten using the DISTINCT keyword to eliminate duplicate rows. SELECT DISTINCT emp_salary FROM employee; EMP_SALARY $25, $25, $30, $30, $38, $38, $43, $43, $55, $55,000.00

Bordoloi and Bock THE WHERE CLAUSE THE WHERE CLAUSE Specific rows can be selected by adding a WHERE clause to the SELECT query.Specific rows can be selected by adding a WHERE clause to the SELECT query. SELECT emp_ssn, emp_last_name, emp_first_name, emp_salary FROM employee WHERE emp_salary >= 35000; EMP_SSN EMP_LAST_NAME EMP_FIRST_NAME EMP_SALARY Bordoloi Bijoy $55, Joyner Suzanne $43, Zhu Waiman $43, more rows will be displayed…

Bordoloi and Bock Comparison Operators Operator Meaning =equal to =equal to <less than <less than >greater than >greater than >=greater than or equal to >=greater than or equal to <=less than or equal to <=less than or equal to !=not equal to !=not equal to <>not equal to <>not equal to !>not greater than !>not greater than !<not less than !<not less than

Bordoloi and Bock Comparing Character Data Comparing Character Data Comparison operators are not limited to numeric data.Comparison operators are not limited to numeric data. They can also be used with columns containing character data.They can also be used with columns containing character data. If the value is a character string or date, you must surround the value (string of characters) with which a column is being compared with single quotation (' ') marks.If the value is a character string or date, you must surround the value (string of characters) with which a column is being compared with single quotation (' ') marks. SELECT emp_ssn, emp_last_name, emp_first_name FROM employee WHERE emp_gender = 'M';

Bordoloi and Bock Comparing Character Data Comparing Character Data SELECT emp_ssn, emp_last_name, emp_first_name FROM employee WHERE emp_gender = M; ERROR at line 3: ORA-00904: invalid column name Since the literal string value was not enclosed by single quote marks, Oracle assumed the letter M to be a column name.Since the literal string value was not enclosed by single quote marks, Oracle assumed the letter M to be a column name. There is no column named M in the table so an error was returned.There is no column named M in the table so an error was returned.

Bordoloi and Bock THE ORDER BY CLAUSE Output from a SELECT statement can be sorted by using the optional ORDER BY clause.Output from a SELECT statement can be sorted by using the optional ORDER BY clause. SELECT emp_last_name, emp_first_name FROM employee WHERE emp_last_name >= 'J' ORDER BY emp_last_name; EMP_LAST_NAME EMP_FIRST_NAME Joshi Dinesh Joyner Suzanne Markis Marcia more rows will be displayed …

Bordoloi and Bock ORDER BY With ASC and DESC To sort columns from high to low, or descending, an optional keyword DESC must be specified.To sort columns from high to low, or descending, an optional keyword DESC must be specified.  ASC - Ascending, low to high.  DESC - Descending, high to low. When ASC or DESC is used, it must be followed by the column name.When ASC or DESC is used, it must be followed by the column name.

Bordoloi and Bock ORDER BY With More Than One Column Sorting by multiple columns improves the look and usability of the information.Sorting by multiple columns improves the look and usability of the information. SELECT emp_dpt_number, emp_last_name, emp_first_name FROM employee ORDER BY emp_dpt_number, emp_last_name; EMP_DPT_NUMBER EMP_LAST_NAME EMP_FIRST_NAME Bordoloi Bijoy 1 Bordoloi Bijoy 3 Amin Hyder 3 Amin Hyder 3 Joyner Suzanne 3 Joyner Suzanne 3 Markis Marcia 3 Markis Marcia 7 Bock Douglas 7 Bock Douglas 7 Joshi Dinesh 7 Joshi Dinesh 7 Prescott Sherri 7 Prescott Sherri 7 Zhu Waiman 7 Zhu Waiman