Working with Columns, Characters and Rows. Copyright 2008, Oracle. All rights reserved.

Similar documents
Database Programming with SQL

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Limit Rows Selected. Copyright 2008, Oracle. All rights reserved.

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

RETRIEVING DATA USING THE SQL SELECT STATEMENT

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

Database Programming with SQL

Database Programming with SQL

Database Programming with SQL

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

Using the Set Operators. Copyright 2006, Oracle. All rights reserved.

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

DUE: 9. Create a query that will return the average order total for all Global Fast Foods orders from January 1, 2002, to December 21, 2002.

Database Programming with SQL

Database Programming with SQL

Database Programming with SQL

GIFT Department of Computing Science. CS-217/224: Database Systems. Lab-5 Manual. Displaying Data from Multiple Tables - SQL Joins

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

SELF TEST. List the Capabilities of SQL SELECT Statements

Cartesian Product and the Join Operations. Copyright 2008, Oracle. All rights reserved.

CIS 363 MySQL. Chapter 12 Joins Chapter 13 Subqueries

Introduction to Oracle

RESTRICTING AND SORTING DATA

Oracle Database 10g: SQL Fundamentals I

Using the Set Operators. Copyright 2004, Oracle. All rights reserved.

Manipulating Data. Copyright 2004, Oracle. All rights reserved.

Architecture. Architecture. Introduction to Oracle 10g Express Edition. Help

DEFAULT Values, MERGE, and Multi-Table Inserts. Copyright 2009, Oracle. All rights reserved.

CIS 207 Oracle - Database Programming and SQL HOMEWORK: # 13

chapter 2 G ETTING I NFORMATION FROM A TABLE

Creating Other Schema Objects

Database Programming with SQL

DUE: CD_NUMBER TITLE PRODUCER YEAR 97 Celebrate the Day R & B Inc Holiday Tunes for All Tunes are US 2004

Assignment Grading Rubric

Oracle Database 10g: SQL Fundamentals I

Intermediate SQL: Aggregated Data, Joins and Set Operators

EXISTS NOT EXISTS WITH

School of Computing and Information Technology Session: Spring CSCI835 Database Systems (Bridging Subject) Sample class test 23 July 2018

Database Programming with PL/SQL

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Database Programming with SQL

Join, Sub queries and set operators

Data Manipulation Language

KORA. RDBMS Concepts II

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Alkérdések II. Copyright 2004, Oracle. All rights reserved.

Database Programming - Section 7. Instructor Guide

SQL STRUCTURED QUERY LANGUAGE

Bsc (Hons) Software Engineering. Examinations for / Semester 1. Resit Examinations for BSE/15A/FT & BSE/16A/FT

Objectives. After completing this lesson, you should be able to do the following:

SQL Structured Query Language Introduction

School of Computing and Information Technology. Examination Paper Autumn Session 2017

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

1Z Oracle Database 11g - SQL Fundamentals I Exam Summary Syllabus Questions

Oracle Database 10g: Introduction to SQL

Getting Information from a Table

Practical 8. SHANKER SINH VAGHELA BAPU INSTITUTE OF TECHNOLGY Subject: - DBMS Branch: - IT/CE Semester: - 3rd. 1. What does SQL stand for?

RMOUG Training Days 2018

Database Programming - Section 10. Instructor Guide

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

Oracle Syllabus Course code-r10605 SQL

Oracle MOOC: SQL Fundamentals

Introduction to Relational Database Concepts. Copyright 2011, Oracle. All rights reserved.

Querying Data with Transact SQL

Sun Certified MySQL Associate

DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

Institute of Aga. Microsoft SQL Server LECTURER NIYAZ M. SALIH

CIS Reading Packet: "Views, and Simple Reports - Part 1"

Additional Practice Solutions

Introduction to Oracle9i: SQL

Tables From Existing Tables

Reference: W3School -

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

Database Programming - Section 3. Instructor Guide

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

Database Foundations. 6-9 Joining Tables Using JOIN. Copyright 2014, Oracle and/or its affiliates. All rights reserved.

1) Introduction to SQL

CPS221 Lecture: Relational Database Querying and Updating

CS 275 Winter 2011 Problem Set 3

Database Management Systems,

CPS221 Lecture: Relational Database Querying and Updating

Oracle Database 10g Express

Creating Other Schema Objects. Copyright 2004, Oracle. All rights reserved.

Database Programming - Section 18. Instructor Guide

CS Reading Packet: "Views, and Simple Reports - Part 1"

ITCertMaster. Safe, simple and fast. 100% Pass guarantee! IT Certification Guaranteed, The Easy Way!

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

Part III. Data Modelling. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 1

Táblák tartalmának módosítása. Copyright 2004, Oracle. All rights reserved.

T-SQL Training: T-SQL for SQL Server for Developers

CSE 530A SQL. Washington University Fall 2013

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

EE221 Databases Practicals Manual

Misc. Triggers Views Roles Sequences - Synonyms. Eng. Mohammed Alokshiya. Islamic University of Gaza. Faculty of Engineering

Unit 1 - Chapter 4,5

Transcription:

Working with Columns, Characters and Rows

What Will I Learn? In this lesson, you will learn to: Apply the concatenation operator to link columns to other columns, arithmetic expressions or constant values to create a character expression Use Column Aliases, to rename columns in the query result Enter literal values of type character, number or date into a SELECT statement Define and use DISTINCT to eliminate duplicate rows Display the structure of a table using DESCRIBE or DESC Edit, execute and save SQL statements in Oracle Application Express 2

Why Learn It? If you were writing an article about the Olympics, you might want to know how many different countries were being represented. Having to go through lists and lists of participant names could be very tedious. Fortunately, using SQL, your job could take less than a minute. In addition, you could format your output to read like a sentence. You will find these SQL features very useful. 3

4 DESCRIBE Use the DESCRIBE (DESC) command to display the structure of a table. You have used DESC to view the structure of the database tables used in this course. DESC returns the table name, table schema, tablespace name, indexes, triggers, constraints and comments as well as the data types, primary and foreign keys and which columns can be nullable.

5 DESCRIBE This is important information when inserting new rows into a table because you must know the type of data each column will accept and whether the column can be left empty. DESC departments; Primary Key Name Type Length Precision Scale Nullable Default Comments 1 DEPARTMENT_ ID Number 4 0 Primary key column of departments table. DEPARTMENT_ NAME Varchar2 30 A not null column that shows name of a department. MANAGER_ID Number 6 0 Manager_id of a department. Foreign key to employee_id column of employees table. The manager_id column of the employee table references this column. LOCATION_ID Number 4 0 Location id where a department is located. Foreign key to location_id column of locations table.

6 CONCATENATION Concatenation means to connect or link together in a series. In SQL, this operator can link columns to other columns, arithmetic expressions or constant values to create a character expression. The concatenation operator is used to create readable text output.

7 CONCATENATION & COLUMN ALIASES Column aliases are useful when using the concatenation operator so that the column heading does not appear as the default SELECT line. When values are concatenated, the resulting value is a character string. The symbol for concatenation is 2 vertical bars sometimes referred to as pipes. Columns on either side of the operator are combined to make a single output column.

CONCATENATION & COLUMN ALIASES In the DJ on Demand example shown, the ' ' was used to make a space between the first and last names. The empty set of single quotation marks created the space. Compare the differences in the text output in the query that used concatenation and the one that did not. DJs on Demand Clients SELECT first_name ' ' last_name AS "DJ on Demand Clients" FROM d_clients; Hiram Peters Serena Jones Lauren Vigil FIRST_NAME DJs on Demand Clients Hiram Peters SELECT first_name, last_name AS "DJ on Demand Clients" FROM d_clients; Serena Lauren Jones Vigil 8

CONCATENATION & COLUMN ALIASES Using concatenation and literal values, you can create output that looks almost like a sentence or statement, by adding literal values. These can be a character, a number or a date that is included in the SELECT list and that is not a column name or a column alias. Characters and dates must be enclosed in a set of single quotes. Every row returned from a query with literal values will have the same character string in it. 9

CONCATENATION & COLUMN ALIASES Can you identify which two words came from table data and which were added by the person doing the query? In the concatenation example, Lorentz has a monthly salary of 4200 dollars, the 'has a monthly salary of' and 'dollars' are examples of literals. If you were to create a SQL statement to produce output in this format, it would be written as: PAY King has a monthly salary of 24000 dollars Kochhar has a monthly salary of 17000 dollars De Haan has a monthly salary of 17000 dollars Hunold has a monthly salary of 9000 dollars Ernst has a monthly salary of 6000 dollars Lorentz has a monthly salary of 4200 dollars..continued SELECT last_name ' has a monthly salary of ' salary ' dollars' AS Pay FROM employees 10

CONCATENATION & COLUMN ALIASES For example, the preceding query could also include numbers as literal values: SELECT last_name ' has a ' 1 ' year salary of ' salary*12 ' dollars' AS Pay FROM employees; PAY King has a 1 year salary of 288000 dollars The table at right shows some of the rows returned: Kochhar has a 1 year salary of 204000 dollars De Haan has a 1 year salary of 204000 dollars Hunold has a 1 year salary of 108000 dollars Ernst has a 1 year salary of 72000 dollars..continued 11

12 DISTINCT Unless you indicate otherwise, the output of a SQL query will display the results without eliminating duplicate rows. There are many times when you want to know how many unique instances of something exist. Remember our original question? We wanted to find out what different countries were being represented in the Olympics. In SQL, the DISTINCT keyword is used to eliminate duplicate rows. SELECT country FROM athlethes COUNTRY China Japan China USA Denmark Sweden France India Denmark

13 DISTINCT In the example shown, DISTINCT would be used if we wanted to know which different countries the Athletes at the Olympics represents. We aren t interested in the number of different countries, only in a list of the different countries. DISTINCT automatically displays the output in alphabetical order. SELECT DISTINCT country FROM athlethes COUNTRY China Denmark France India Japan Sweden USA

14 DISTINCT The DISTINCT qualifier affects all listed columns and returns every distinct combination of the columns in the SELECT clause. The keyword DISTINCT must appear directly after the SELECT keyword. What do you think would be returned if we had the athletes from Denmark and Sweden having three different shirt colors? SELECT DISTINCT country, color FROM athletes;

15 EXECUTE, SAVE AND EDIT STATEMENTS IN OAE Now that you have been using OAE to create and execute statements, would it not be nice if you could save those statements for later, so you could run them again or perhaps edit them slightly and then save a new copy of the statement? Luckily for you, OAE has facilities to do just that. Your teacher will demonstrate these facilities for you, and you can find further information in the Oracle Application Express User Guide.

Summary In this lesson, you have learned about: How to apply the concatenation operator to link columns to other columns When to use literal values of type character, number or date in a SELECT statement How to eliminate duplicate rows by using DISTINCT Display the structure of a table using DESCRIBE or DESC How to edit, execute and save SQL statements in Oracle Application Express 16

17 Summary Practice Guide The link for the lesson practice guide can be found in the course outline.