Oracle Academy: Database Programming with SQL (PDF)
Document Details
Uploaded by StrikingWilliamsite7843
Oracle Academy
2020
Tags
Summary
This document from Oracle Academy provides a detailed overview of database programming with SQL, covering essential topics like SQL statements, keywords, and clauses. This lesson provides guidelines for writing effective queries and covers the practical application of SQL through examples. It touches upon various SQL concepts and illustrates database operations.
Full Transcript
Database Programming with SQL 1-3 Anatomy of a SQL Statement Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Objectives This lesson covers the following objectives: −Match projection and selection with their correct capabilities −Create a ba...
Database Programming with SQL 1-3 Anatomy of a SQL Statement Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Objectives This lesson covers the following objectives: −Match projection and selection with their correct capabilities −Create a basic SELECT statement −Use the correct syntax to display all rows in a table −Use the correct syntax to select specific columns in a table, modify the way data is displayed, and perform calculations using arithmetic expressions and operators DP 1-3 3 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Objectives This lesson covers the following objectives: −Formulate queries using correct operator precedence to display desired results −Define a null value −Demonstrate the effect null values create in arithmetic expressions −Construct a query using a column alias DP 1-3 4 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement SELECT Keyword SELECT is one of the most important, if not the most important, keyword in SQL You use SELECT to retrieve information from the database. When you learn how to use SELECT, you've opened the door to the database Imagine a database containing information about movies such as title, genre, studio, producer, release date, series, country, language, rating, running time, and so on What if you only wanted the titles of movies created in India? The SELECT statement allows you to search for specific data DP 1-3 5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement SELECT Statement The SELECT statement retrieves information from the database The syntax for a SELECT statement is as follows: SELECT FROM ; In its simplest form, a SELECT statement must include the following: −A SELECT clause, which specifies the columns to be displayed −A FROM clause, which specifies the table containing the columns listed in the SELECT clause DP 1-3 6 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Conventions Throughout this course, the following will be used: SELECT last_name FROM employees; A keyword refers to an individual SQL command For example, SELECT and FROM are keywords A clause is a part of a SQL statement For example, SELECT last_name is a clause A statement is a combination of two or more clauses For example, SELECT last_name FROM employees is a statement DP 1-3 7 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Capabilities of SELECT Statements Projection: −Used to choose columns in a table Selection: −Used to choose rows in a table Table 2: Projection Table 2: Selection DP 1-3 8 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Projection and Selection ID FIRST_NAME LAST_NAME SALARY 10 John Doe 4000 20 Jane Jones 3000 30 Sylvia Smith 5000 40 Hai Nguyen 6000 Projection SELECT salary Selection FROM employees WHERE last_name LIKE 'Smith'; DP 1-3 9 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Selecting All Columns You can display all of the columns of data in a table by using an asterisk symbol (*) instead of a column name in the SELECT clause In the example SELECT * shown, all of the FROM countries; columns in the countries table COUNTRY_ID COUNTRY_NAME REGION_ID are selected CA Canada 2 DE Germany 1 UK United Kingdom 1 US United States of America 2 DP 1-3 10 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Selecting All Columns You can also display all the columns in a table by listing them individually SELECT country_id, country_name, region_id FROM countries; COUNTRY_ID COUNTRY_NAME REGION_ID CA Canada 2 DE Germany 1 UK United Kingdom 1 US United States of America 2 DP 1-3 11 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Projecting Specific Columns If you want to PROJECT only specific columns from a table to be displayed, simply list each of the column names you want and separate each name with a comma in the SELECT clause SELECT location_id, city, state_province FROM locations; LOCATION_ID CITY STATE_PROVINCE 1800 Toronto Ontario 2500 Oxford Oxford 1400 Southlake Texas 1500 South San Francisco California 1700 Seattle Washington DP 1-3 12 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Using Arithmetic Operators Using a few simple rules and guidelines, you can construct SQL statements that are both easy to read and easy to edit Knowing the rules will make learning SQL easy You may need to modify the way in which data is displayed, perform calculations, or look at what-if scenarios For example, "What if every employee was given a 5% raise? How would that affect our yearly profit figures?" DP 1-3 13 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Using Arithmetic Operators These types of calculations are all possible using arithmetic expressions You are already familiar with arithmetic expressions in mathematics: −add (+), subtract (-) , multiply (*) and divide (/) Note that this example does not create new columns in the tables or change the actual data values The results of the calculations will appear only in the output DP 1-3 14 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Using Arithmetic Operators The example shown uses the addition operator to calculate LAST_NAME SALARY SALARY+300 a salary increase of 300 for all King 24000 24300 employees and displays a new Kochhar 17000 17300 SALARY + 300 column in the De Haan 17000 17300 output Whalen 4400 4700 SELECT last_name, salary, Higgins 12000 12300 salary + 300 FROM employees; Gietz 8300 8600 Putting in blank spaces before Zlotkey 10500 10800 and after an arithmetic Abel 11000 11300 operator will not affect the Taylor 8600 8900 output Grant 7000 7300 DP 1-3 15 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Precedence in Arithmetic Operators Precedence is the order in which Oracle evaluates different operators in the same expression When evaluating an expression containing multiple operators, Oracle evaluates operators with higher precedence before evaluating those with lower precedence Oracle evaluates operators with equal precedence from left to right within an expression DP 1-3 16 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Precedence in Arithmetic Operators Arithmetic operators perform the mathematical operations of Multiplication, Division, Addition, and Subtraction If these operators appear together in an expression, multiplication and division are evaluated first. So the order is: * / + - An easy way to remember their operator precedence is the mnemonic device: My Dear Aunt Sally DP 1-3 17 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Precedence in Arithmetic Operators If operators within an expression are of the same priority, then evaluation is done from left to right You can always use parentheses to force the expression within parentheses to be evaluated first In the example tables shown on the next slide, note the differences in the output between the query that used parentheses and the one that didn't DP 1-3 18 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Precedence in Arithmetic Operators Operator Precedence Using Parentheses SELECT last_name, salary, SELECT last_name, salary, 12*salary +100 12*(salary +100) FROM employees; FROM employees; LAST_NAME SALARY 12*SALARY+100 LAST_NAME SALARY 12*(SALARY+100) King 24000 288100 King 24000 289200 Kochhar 17000 204100 Kochhar 17000 205200 De Haan 17000 204100 De Haan 17000 205200 Whalen 4400 52900 Whalen 4400 54000 Higgins 12000 144100 Higgins 12000 145200 Gietz 8300 99700 Gietz 8300 100800 DP 1-3 19 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement NULL Values In SQL, NULL is an interesting word To understand NULL, you have to know what NULL is and what NULL is not NULL is a value that is unavailable, unassigned, unknown, or inapplicable NULL is not the same as a zero or a space In SQL, a zero is a number, and a space is a character DP 1-3 20 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement NULL Values Sometimes, you don't know the value for a column In a database, you can store unknowns in your databases Relational databases use a placeholder called NULL or null to represent these unknown values DP 1-3 21 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement NULL Values If any column value in Salaries and Commissions an arithmetic LAST_NAME JOB_ID SALARY COMMISSION_PCT expression is null, the King AD_PRES 24000 - result is null or Kochhar AD_VP 17000 - unknown De Haan AD_VP 17000 - If you try to divide by Whalen AD_ASST 4400 - null, the result is null or Higgins AC_MGR 12000 - unknown Gietz AC_ACCOU NT 8300 - However, if you try to Zlotkey SA_MAN 10500.2 divide by zero, you get Abel SA_REP 11000.3 an error DP 1-3 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 22 Anatomy of a SQL Statement NULL Values SELECT last_name, job_id, salary, commission_pct, salary*commission_pct FROM employees; Salaries and Commissions LAST_NAME JOB_ID SALARY COMMISSION_PCT SALARY*COMMISSION_PCT King AD_PRES 24000 - - Kochhar AD_VP 17000 - - De Haan AD_VP 17000 - - Whalen AD_ASST 4400 - - Higgins AC_MGR 12000 - - Gietz AC_ACCOUNT 8300 - - Zlotkey SA_MAN 10500.2 2100 Abel SA_REP 11000.3 3300 Taylor SA_REP 8600.2 1720 DP 1-3 23 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Aliases An Alias is a way of renaming a column heading in the output Without aliases, when the result of a SQL statement is displayed, the name of the columns displayed will be the same as the column names in the table or a name showing an arithmetic operation such as 12*(SALARY + 100) You probably want your output to display a name that is easier to understand, a more "friendly" name Column aliases let you rename columns in the output DP 1-3 24 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Aliases There are several rules when using column aliases to format output A column alias: −Renames a column heading −Is useful with calculations −Immediately follows the column name −May have the optional AS keyword between the column name and alias −Requires double quotation marks if the alias contains spaces or special characters, or is case-sensitive DP 1-3 25 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Using Column Aliases The syntax for aliases is: SELECT * |column|expr [ AS alias],..... FROM table; Examples: SELECT last_name AS name, NAME COMM commission_pct AS comm King - FROM employees; Kochhar - De Haan - SELECT last_name "Name", Name Annual Salary salary*12 "Annual Salary" King 288000 FROM employees; Kochhar 204000 De Haan 204000 DP 1-3 26 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Terminology Key terms used in this lesson included: −Arithmetic expression −Arithmetic operator −Clause −Column −Column alias −From clause −NULL DP 1-3 27 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Terminology Key terms used in this lesson included: −Projection −Select clause −Selection −Select statement −Statement −WHERE Clause − * (Asterisk) DP 1-3 28 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Summary In this lesson, you should have learned how to: −Match projection and selection with their correct capabilities −Create a basic SELECT statement −Use the correct syntax to display all rows in a table −Use the correct syntax to select specific columns in a table, modify the way data is displayed, and perform calculations using arithmetic expressions and operators DP 1-3 29 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement Summary In this lesson, you should have learned how to: −Formulate queries using correct operator precedence to display desired results −Define a null value −Demonstrate the effect null values create in arithmetic expressions −Construct a query using a column alias DP 1-3 30 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Anatomy of a SQL Statement