Database Programming with SQL

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

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.

Database Programming with SQL

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

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

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

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

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

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

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

Database Programming with SQL

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

SELF TEST. List the Capabilities of SQL SELECT Statements

Database Programming with SQL

Manipulating Data. Copyright 2004, Oracle. 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.

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

Database Programming with SQL

Assignment Grading Rubric

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

Oracle Database 10g: SQL Fundamentals I

Data Manipulation Language

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

Join, Sub queries and set operators

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

EXISTS NOT EXISTS WITH

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

Oracle Database 10g: SQL Fundamentals I

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

Intermediate SQL: Aggregated Data, Joins and Set Operators

Database Programming with SQL

Oracle Database SQL Basics

Getting Information from a Table

chapter 2 G ETTING I NFORMATION FROM A TABLE

Database Foundations. 6-3 Data Definition Language (DDL) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

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

Creating Other Schema Objects

DATA CONSTRAINT. Prepared By: Dr. Vipul Vekariya

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

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

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

CS 275 Winter 2011 Problem Set 3

Database Programming with PL/SQL

Introduction to Oracle9i: SQL

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

ÇALIŞMA TEST SORULARI

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

Retrieving Data from Multiple Tables

Advance Database Systems. Joining Concepts in Advanced SQL Lecture# 4

Database Management Systems,

KORA. RDBMS Concepts II

Additional Practice Solutions

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

CSC Web Programming. Introduction to SQL

Unit 1 - Chapter 4,5

Database Programming with PL/SQL

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

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

Updating Column Values and Deleting Rows. Copyright 2008, Oracle. All rights reserved.

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

Tables From Existing Tables

Sun Certified MySQL Associate

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

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

Introduction to Explicit Cursors. Copyright 2008, Oracle. All rights reserved.

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

RESTRICTING AND SORTING DATA

SQL Structured Query Language Introduction

Database Programming - Section 7. Instructor Guide

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

Oracle Database 10g: Introduction to SQL

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

Oracle MOOC: SQL Fundamentals

HR Database. Sample Output from TechWriter 2007 for Databases

Using DbVisualizer Variables

Course Overview. Copyright 2010, Oracle and/or its affiliates. All rights reserved.

Introduction to Oracle

Graphical Joins in More Detail

BraindumpsVCE. Best vce braindumps-exam vce pdf free download

SYSTEM CODE COURSE NAME DESCRIPTION SEM

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

CIS 363 MySQL. Chapter 12 Joins Chapter 13 Subqueries

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

Database Programming with PL/SQL

Basic SQL. Basic SQL. Basic SQL

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Database Programming - Section 18. Instructor Guide

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

Database Programming with PL/SQL

Oracle Database 11g: SQL Fundamentals I

SQL Data Manipulation Language. Lecture 5. Introduction to SQL language. Last updated: December 10, 2014

What Are Group Functions? Reporting Aggregated Data Using the Group Functions. Objectives. Types of Group Functions

Oracle Syllabus Course code-r10605 SQL

Database Management Systems,

Transcription:

Database Programming with SQL 2-1

Objectives This lesson covers the following objectives: 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 Edit, execute, and save SQL statements in Oracle Application Express 3

Purpose If you were writing an article about the Olympics, you might want to know how many different countries and how many different athletes from each country 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. 4

DESCRIBE Use the DESCRIBE (DESC) command to display the structure of a table. DESCRIBE <table_name>; DESC returns the table name, data types, primary and foreign keys, and nullable columns, as well as other object details that will be covered later in the course. An example of using the DESCRIBE command is shown on the next slide. 5

DESCRIBE DESC departments; Table Name Type Length Precision Scale Primary Key Nullable Default Comments DEPAR TMENT S DEPARTMENT _ID DEPARTMENT _NAME Number 4 0 1 Primary key column of departments table. 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... LOCATION_ID Number 4 0 Location id where a department is located. Foreign key to location_id column of locations table. 6

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 or not the column can be left empty. 7

The Concatenation Operator Concatenation means to connect or link together in a series. The symbol for concatenation is 2 vertical bars sometimes referred to as "pipes." Values on either side of the operator are combined to make a single output column. The syntax is: string1 string2 string_n When values are concatenated, the resulting value is a character string. 8

The Concatenation Operator In SQL, the concatenation 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. In the following example, the department_id is concatenated to the department_name. SELECT department_id department_name FROM departments; DEPARTMENT_ID DEPARTMENT_NAME 10Administration 20Marketing 50Shipping 60IT 9

The Concatenation Operator In this variation on the previous example, the '' is used to make a space between the department_id and department_name. The 'space'character in single quotation marks creates a space between the column values. SELECT department_id '' department_name FROM departments; DEPARTMENT_ID '' DEPARTMENT_NAME 10 Administration 20 Marketing 50 Shipping 60 IT 10

Concatenation and Column Aliases Column aliases are useful when using the concatenation operator so that the default SELECT line does not appear as the column heading. SELECT department_id '' department_name AS " Department Info " FROM departments; Department Info 10 Administration 20 Marketing 50 Shipping 60 IT SELECT first_name '' last_name AS "Employee Name" FROM employees; Employee Name Ellen Abel Curtis Davies Lex De Haan 11

Concatenation and Literal Values A literal value is a fixed data value such as a character, number, or date. The following are examples of literal values: 'dollars' 1000 'January 1, 2009' Using concatenation and literal values, you can create output that looks like a sentence or statement. 12

Concatenation and Literal Values Literal values can be included in the SELECT list with the concatenation operator. 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. 13

Concatenation and Literal Values In the following example, King earns 24000 dollars a month. The strings, '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: SELECT last_name 'has a monthly salary of ' salary 'dollars.'as Pay FROM employees; 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. Whalen has a monthly salary of 4400 dollars. Higgins has a monthly salary of 12000 dollars. Gietz has a monthly salary of 8300 dollars. 14

Concatenation and Literal Values Note the space character following the opening quote and preceding the ending quote. What happens if you remove the spaces? 15

Concatenation and Literal Values You can also include numbers as literal values. In the following example, the number 1 is concatenated to the strings, 'has a 'and 'year salary of '. 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. Kochhar has a 1 year salary of 204000 dollars. De Haan has a 1 year salary of 204000 dollars. Whalen has a 1 year salary of 52800 dollars. Higgins has a 1 year salary of 144000 dollars. 16

Using DISTINCT to Eliminate Duplicate Rows Many times, you will want to know how many unique instances of something exist. For example, what if you wanted a list of all of the departments for which there are employees. You could write a query to select the department_ids from the employees table: SELECT department_id FROM employees; DEPARTMENT_ID 90 90 90 10 110 110 80 80 80 17

Using DISTINCT to Eliminate Duplicate Rows Note all of the duplicate rows. How can you modify the statement to eliminate the duplicate rows? SELECT department_id FROM employees; DEPARTMENT_ID 90 90 90 10 110 110 80 80 80 18

Using DISTINCT to Eliminate Duplicate Rows Unless you indicate otherwise, the output of a SQL query will display the results without eliminating duplicate rows. In SQL, the DISTINCT keyword is used to eliminate duplicate rows. SELECT DISTINCT department_id FROM employees; 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. DEPARTMENT_ID 90 20 110 80 50 10 60 19

EXECUTE, SAVE, and EDIT in Oracle Application Express Now that you have been using Oracle Application Express to create and execute statements, it would 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. Oracle Application Express 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. 20

Terminology Key terms used in this lesson included: DESCRIBE Concatenation Operator Literal Values DISTINCT 21

Summary In this lesson, you should have learned how 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 Edit, execute, and save SQL statements in Oracle Application Express 22