Download presentation
Presentation is loading. Please wait.
1
Sections 1 - SQL and other basic statements
SQL Review Sections 1 - SQL and other basic statements
2
Using APEX SQL editor You may either type the command into the SQL editor or use the cut and paste option If you are going to cut/paste the command copy the command from the word document into NotePad. This will drop out hidden characters. Next copy the command from the NotePad into the editor Have student open/save the Basic SQL commands in section 0 for section 13. Point out the following links/documents in section 16 that they should download. Students should also print out the excel sheet for the 3 main tables in APEX from the website. Marge Hohly
3
Enter SQL command Marge Hohly
4
Display the Table structure
Enter the following command: DESCRIBE MUSIC; The structure of the table should be shown. Slide 5: Tell Me /Show Me – The DESCRIBE command displays the structure … Syntax- The rules governing the formation of statements in a programming language. Write the DESCRIBE music syntax and ask students to enter the SQL command. Discuss what the statement means and what it returns. Run command. Review APEX where you see tables etc. Marge Hohly
5
Select command structure
SELECT field1, field2, field3 FROM table_name WHERE condition; Try the following command SELECT employee_id, first_name, last_name, department_id FROM employees; SELECT employee_id, first_name, last_name, department_id FROM employees WHERE department_id = 90; Try the above commands Slide 6: Tell Me /Show Me – The SELECT * command returns all the rows… Write the SELECT * FROM music syntax on the board. Ask students to enter the SQL command. Discuss what the statement means and what it returns. Slide 7: Tell Me /Show Me – To return a subset of the data, modify the SELECT … Explain that the <condition> often takes the form of column name + operator (=, >, <, IS NULL, IS NOT NULL, etc.). Marge Hohly
6
SQL DESCRIBE DESCRIBE <table name>;
DESCRIBE employees; Try the last statement. Marge Hohly
7
Insert Data Note the data types for each column
Inserting data into the table. Since the table is empty all fields need to be populated, so column names can be omitted INSERT INTO music VALUES (10,'Marge Hohly','Folk'); This will insert one record into the table Next display the contents of the table to view the data SELECT * FROM music; Point out format and structure: Reserved words all caps Fields (attributes) lower case End with ; Marge Hohly
8
Typical error messages
The following statement has a spelling error: SELCT * FROM employees; The error message is: ORA-00900: invalid SQL statement The following statement incorrectly names the table employee instead of employees: SELECT * FROM employee; The error message is: ORA-00942: table or view does not exist Run the correct statement. SELECT * FROM employees; Slide 1: Anatomy of a SQL Statement What to Watch For This may be a student's first time using SQL. Insist on a standard for coding statements and demonstrate it early in this lesson. If you want statements of separate lines, show students your standard. At the beginning, students will be frustrated when their queries don't return what they expected. It is helpful to code several incorrect SQL queries and demonstrate what error messages are returned. This will help students become more independent and help reduce the "it doesn't work" statements. For instance: The following statement has a spelling error: SELCT * FROM employees The error message is: ORA-00900: invalid SQL statement The following statement incorrectly names the table employee instead of employees: SELECT * FROM employee The error message is: ORA-00942: table or view does not exist Marge Hohly
9
Subset of data WHERE clause
SELECT <column name 1, column name 2, etc.> FROM <table name> WHERE <condition>; SELECT first_name, last_name, salary FROM employees WHERE salary > 5000; 13.1.5 Explain that the <condition> often takes the form of column name + operator (=, >, <, IS NULL, IS NOT NULL, etc.)." Marge Hohly
10
Application Express SQL editor
The SQL course will use the three following sets of database tables for examples and practice exercises. Oracle tables: COUNTRIES, REGIONS, DEPARTMENTS, EMPLOYEES, JOBS, JOB_HISTORY AND JOB_GRADES DJs on Demand database tables: D_CDS, D_PACKAGES, D_TYPES, D_THEMES, D_CLIENTS, D_VENUES, D_SONGS, D_TRACK_LISTINGS, D_PARTNERS, D_EVENTS, D_PLAY_LIST_ITEMS, D_JOB_ASSIGNMENTS Global Fast Foods database tables: F_CUSTOMERS, F_REGULAR_MENUS, F_PROMOTIONAL_MENUS, F_SHIFTS, F_STAFFS, F_FOOD_ITEMS, F_ORDERS, F_ORDER_LINES, F_SHIFT_ASSIGNMENTS Print out these tables for your reference when using the Application Express editor These tables are available on the Student Resource web page for this class Marge Hohly
11
Review the tables There are six properties of tables in a relational database: Property 1: Entries in columns are single-valued. Property 2: Entries in columns are of the same kind. Property 3: Each row is unique. Property 4: Sequence of columns is insignificant. Property 5: Sequence of rows is insignificant. Property 6: Each column has a unique name. Marge Hohly
12
Categories of SQL Statements
Data manipulation language (DML) statements Begin with INSERT, UPDATE, DELETE, or MERGE Used to modify the table by entering new rows, changing existing rows, or removing existing rows. Data definition language (DDL) statements set up, change, and remove data structures from the database. The keywords CREATE, ALTER, DROP, RENAME, and TRUNCATE begin DDL statements. Transaction control (TCL) statements are used to manage the changes made by DML statements. Changes to the data are executed using COMMIT, ROLLBACK, and SAVEPOINT. TCL changes can be grouped together into logical transactions. Data control language (DCL) keywords GRANT and REVOKE are used to give or remove access rights to the database and the structures within it. Data retrieval -- Check the balance of a checking/savings account. Data manipulation -- Change an address that the bank uses to notify a customer that an online bank statement has been posted. Data definition -- Create a new online account; the bank can alter data in an account or drop an account that is delinquent. Transaction control language -- Arrange to have a bill paid online and then decide later to change the date originally set for payment. Online bill paying usually has a SUBMIT(COMMIT) option after several transactions have been entered. Data control language -- The bank can revoke an overdrawn account or a credit card issued for failure to make the payment obligations. The bank can grant other users access to an account if requested by the owner of the account. Marge Hohly
13
KEYWORD, CLAUSE, STATEMENT
Throughout this course, the words keyword, clause, and statement are used as follows: A keyword refers to an individual SQL element. For example, SELECT and FROM are keywords. A clause is a part of a SQL statement. SELECT employee_id, last_name, is a clause. A statement is a combination of two or more clauses. SELECT * FROM employees; is a SQL statement. 16.5.x Marge Hohly
14
Selection vs. Projection
SELECT salary FROM employees WHERE last_name like ‘Smith’; Selection (row) Projection (column) ID First_name Last_name salary 10 John Doe 4000 20 Jane Jones 3000 30 Sylvia Smith 5000 40 Hai Nguyen 6000 Slide 7: Tell Me /Show Me – A SELECT statement can do the following: Projection- The capability in SQL to choose the columns in a table that you want returned from a query Selection- The capability in SQL to choose the rows in a table returned from a query Marge Hohly
15
Join Slide 8: Tell Me /Show Me – Join: Used to bring together data that is stored … Join- Display data from two or more related tables Marge Hohly
16
SELECT statement SELECT statements can provide the same information depending on how they are written Example: SELECT * FROM d_songs; SELECT id, title, duration, artist, type_code FROM d_songs; Slide 9: Tell Me /Show Me – Selecting all Columns Take time for students to practice each new keyword. Warn students that sometimes doing a SELECT * FROM table can take a long time to return the data. It all depends on the number of rows stored in that particular table. Remember, Oracle tables can store millions of rows of data. Break this content into show and practice. Explain one SQL keyword/statement at a time, and then have the students practice. Slide 10: Tell Me / Show Me – Selecting all Columns (continued) Demonstrate coding conventions to the students. All queries can be in lowercase, but for clarity and ease of reading, you may want to have keywords typed in UPPERCASE. It makes finding student mistakes easier. Also make sure that each keyword starts on a new line, as this further eases reading and de-bugging of the code students are producing. All examples on the slides have a semicolon after each statement. Oracle Application Express does not require this syntax, but other Oracle SQL Interfaces do, so we have included them in the courseware. It may be worth mentioning or demonstrating semicolon not being required in Oracle Application Express, but other Oracle tools. Marge Hohly
17
SELECTION SELECT * FROM employees WHERE department_id = 60;
SELECT * FROM employees WHERE salary > 10000; Selection is a selection of rows, all fields Marge Hohly
18
Projections A subset of columns
SELECT first_name, last_name, salary FROM employees; SELECT id, title, artist FROM d_songs; Marge Hohly
19
Arithmetic Expressions
Create expressions with number and date data by using arithmetic operators. Operator Description + Add - Subtract * Multiply / Divide 5(5*6) + 20 SELECT last_name, salary, 12*salary+100 rewrite as 12*(SALARY + 100) FROM employees; Marge Hohly
20
Operator Precedence () ^ * / + - 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. Remember: Please excuse my dear aunt Sally () ^ * / + - Slide 16: Tell Me / Show Me – Using Arithmetic Operators If operators within an expression are of the same priority, then evaluation is done from left to right. Explain using the following example: Operators that appear in the same group have the same precedence. Which means in the multiplicative group that multiplication is done before division which is done before percentage. In the additive group, addition is done before subtraction. You can always use parentheses to force the expression within parentheses to be evaluated first. In the example 5(5*6) + 20, the expression (5*6) is processed first, that value (30) is multiplied by 5 to equal 150, and then 20 is added to the result. Rewrite the SQL statement shown to produce the results shown in the Operator Precedence table. SELECT last_name, salary, 12*salary+100 rewrite as 12*(SALARY + 100) FROM employees; Marge Hohly
21
What is null? If a row lacks the data value for a particular column, that value is said to be null, or to contain a null. A null is a value that is unavailable, unassigned, unknown, or inapplicable. A null is not the same as zero. Zero is a number. A null is not a space. Space is a character. Slide 20: Tell Me / Show Me – Null Values NULL- A value that is unavailable, unassigned, unknown, or inapplicable Marge Hohly
22
Column Alias Renames a column heading
Is useful in naming columns of derived values Immediately follow the column name Uses optional AS keyword between the column name and alias Required double quotation marks if it contains spaces or special characters or is case sensitive Marge Hohly
23
Using Aliases NAME SALARY Annual Salary Whalen 4400 52800 Hartstein
SELECT last_name name, salary AS Salary, salary*12 “Annual Salary” FROM employees; NAME SALARY Annual Salary Whalen 4400 52800 Hartstein 13000 156000 Fay 6000 72000 Explain the purpose of an alias. Your boss would probably not recognize what a column name such as DEPT_NO means. It would be your job to create a report with the column name "Department Number " instead. Explain that double quotation marks are required for aliases longer than one word or when the format of the alias is anything other than the default uppercase name (e.g., Department Name, or department, or dept.) Explain that the pipe (|) means OR in this example. The pipe key is the caps lock position above the backslash (\) on most keyboards. Marge Hohly
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.