Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.

Slides:



Advertisements
Similar presentations
Structured Query Language (SQL)
Advertisements

Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.
Sorting Rows. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Construct a query to sort a results set in ascending.
Let’s try Oracle. Accessing Oracle The Oracle system, like the SQL Server system, is client / server. For SQL Server, –the client is the Query Analyser.
Functions and string manipulation Lab 2 Week 2. Date functions Commonly used date functions are: –sysdate –next_day –add_months –last_day –months_between.
Databases Lab 5 Further Select Statements. Functions in SQL There are many types of functions provided. The ones that are used most are: –Date and Time.
Databases Week 1, lab 2 Simple selects. About the environment We are using SQL Server for the moment. The server we are using is: –Cian.student.comp.dit.ie.
DT211 Stage 2 Databases Lab 1. Get to know SQL Server SQL server has 2 parts: –A client, running on your machine, in the lab. You access the database.
1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
Single-Row Functions. SQL Functions Functions are a very powerful feature of SQL and can be used to do the following: Perform calculations on data Modify.
Rationale Aspiring Database Developers should be able to efficiently query and maintain databases. This module will help students learn the Structured.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
SQL/lesson 2/Slide 1 of 45 Retrieving Result Sets Objectives In this lesson, you will learn to: * Use wildcards * Use the IS NULL and IS NOT NULL keywords.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
SINGLE-ROW FUNCTIONS Lecture 9. SQL Functions Functions are very powerful feature of SQL and can be used to do the following:  Perform a calculation.
Module 1: Introduction to Transact-SQL
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
IFS Intro to Data Management Chapter 5 Getting More Than Simple Columns.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
Conversion Functions.
SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 1.
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.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
1 Creating and Maintaining Database Objects Part 1 Database Systems.
INTRODUCTION TO SQL Chapter SELECT * FROM teacher WHERE INSTR (subject_id, ‘&1’)= 4 AND LOWER (subject_id) LIKE ‘HST%’ ; When prompted for the.
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
Introduction to Functions – Single Row Functions.
Populating and Querying tables Insert, Update, Delete and View (DML)
Single-Table Queries 2: Advanced Topics CS 320. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data SELECT field1, field2,
ORDER BY clause in SELECT command: Normally, the result of the query will not be in ordered format. If we want to get the result of the query in specific.
1 DBS201: More on SQL Lecture 2. 2 Agenda Select command review How to create a table How to insert data into a table.
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
College of Information Technology, Universiti Tenaga Nasional1 Lab 2: Single-row Functions CISB224 01A CCSB244 01A Semester I, 2008/2009.
Rules of Precedence The rules of precedence determine the order in which expressions are evaluated and calculated. The next table lists the default order.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
1 Section 3 - Select Statement u The Select statement allows you to... –Display Data –Specify Selection Criteria –Sort Data –Group Data for reporting –Use.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Creating Database Objects
Web Systems & Technologies
Chapter 5 Introduction to SQL.
In this session, you will learn to:
Connect to SQL Server and run select statements
Writing Basic SQL SELECT Statements
Basic select statement
Introduction To Structured Query Language (SQL)
Writing Basic SQL SELECT Statements
Section 4 - Sorting/Functions
Creating Database Objects
MySQL SQL for MySQL (I) Salim Mail :
Introduction to SQL Server and the Structure Query Language
Lab 2: Retrieving Data from the Database
Presentation transcript:

Databases Tutorial 2 Further Select Statements

Objectives for Week Data types Sort retrieved data Formatting output

Some additions to tutorial 1 You do not need to use quotes for numeric fields: –Select * from Product where UnitPrice <5 Note the format of the SELECT statement SELECT [{columnname}]/* FROM table-name [WHERE condition] Both SELECT and FROM are always needed.

Sorting The order by clause will allow you to sort the fields. The default order is ascending. To change it to descending, use the keyword desc. select * from categories order by categoryName is the same as select * from categories order by categoryName asc To use descending order: select * from categories order by categoryName desc

To put a name on a column Transact SQL allows the use of the ‘as’ clause to give a name to a column. –Unitprice as Price or –UnitPrice as ‘Unit Price’ This can be used on any column, but is especially useful in a derived column. New columns can be derived from existing fields: E.g. the value of an item in stock is the number in stock by the unit price.

Cast and CONVERT CAST and CONVERT Explicitly converts an expression of one data type to another. CAST and CONVERT provide similar functionality. This can be used to format output and convert data fields from one format to another

Cast and Convert CAST and CONVERT –Explicitly converts an expression of one data type to another. CAST and CONVERT provide similar functionality. Syntax –Using CAST: –CAST ( expression AS data_type ) –Using CONVERT: –CONVERT ( data_type [ ( length ) ], expression [, style ] )

Number conversion When data types are converted with a different number of decimal places, the value is truncated to the most precise digit. –For example, the result of SELECT CAST( AS int) is 10. When data types in which the target data type has fewer decimal points than the source data type are converted, the value is rounded. – For example, the result of CAST( AS money) is $

B. Use CAST with arithmetic operators This example calculates a single column computation (Copies) by dividing the total year- to-date sales (ytd_sales) by the individual book price (price). This result is converted to an int data type after being rounded to the nearest whole number. –USE pubs –GO –SELECT CAST(ROUND(ytd_sales/price, 0) AS int) AS 'Copies' FROM titles GO

C. Use CAST to concatenate This example concatenates noncharacter, nonbinary expressions using the CAST data type conversion function. –USE pubs –GO –SELECT 'The price is ' + CAST(price AS varchar(12)) FROM titles WHERE price > –GO

D. Use CAST for more readable text This example uses CAST in the select list to convert the title column to a char(50) column so the results are more readable. –USE pubs –GO –SELECT CAST(title AS char(50)), ytd_sales FROM titles WHERE type = 'trad_cook' –GO

E. Use CAST with LIKE clause This example converts an int column (the ytd_sales column) to a char(20) column so that it can be used with the LIKE clause. –USE pubs –GO –SELECT title, ytd_sales FROM titles WHERE CAST(ytd_sales AS char(20)) LIKE '15%' AND type = 'trad_cook' –GO

Syntax of CAST Using CAST: CAST ( expression AS data_type ) This allows us to do the following: –Format the output so that it is more readable. –Change a number to a character string. –Change a character string to a numeric format.

Using CAST

Exercises Retrieve the system date, using the ‘GetDate’ function. Convert it into char(12) format. Display it using style 3. Now display it using styles 1 through 14, noting the differences. Which of these formats do you think would be most useful in this country and for what purposes?

Exercises (Northwind) To demonstrate the effectiveness of ‘order by’: –Select all from the order details table –Select all from the order details table ordered by product id. –Select all from the order details table in descending product id order. –Show the order id, the customer id and the day, month and year of the order date, for all orders, in customer id order.

Exercises Amend the previous exercises to put names on the columns. Format your outputs so that they are easy and pleasant to read.

Achievements Writing Basic SQL Select Statements –[√]List the capabilities of SQL SELECT statements –[ √ ]Execute a basic SELECT statement Restricting and Sorting Data –[ √] Limit the rows retrieved by a query –[ √] Sort the rows retrieved by a query Single-Row Functions –[ √] Use conversion functions