CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP.

Slides:



Advertisements
Similar presentations
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
Advertisements

Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved.
CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP.
5 Copyright © 2007, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
SQL server Section 4&5.
Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
SQL Tutorials To understand some of the topics please analyze the following tutorials: The following tutorials will help:
Computer Science 101 Web Access to Databases SQL – Extended Form.
Enhancements to the GROUP BY Clause Fresher Learning Program January, 2012.
Introduction to SQL J.-S. Chou Assistant Professor.
Rationale Aspiring Database Developers should be able to efficiently query and maintain databases. This module will help students learn the Structured.
1 IT420: Database Management and Organization SQL - Data Manipulation Language 27 January 2006 Adina Crăiniceanu
Chapter 3 Single-Table Queries
Introduction to Databases Chapter 7: Data Access and Manipulation.
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.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Using Special Operators (LIKE and IN)
Subqueries.
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP.
SQL-5 (Group By.. Having). Group By  Need: To apply the aggregate functions to subgroups of tuples in a relation, where the subgroups are based on some.
STRUCTURED QUERY LANGUAGE SQL-II IST 210 Organization of Data IST210 1.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
1 SQL SQL (Structured Query Language) : is a database language that is used to create, modify and update database design and data. Good Example of DBMS’s.
CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Web Programming MySql JDBC Web Programming.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
SQL: Single Table Queries SELECT FROM WHERE ORDER D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 1.
Introduction to Database SEM I, AY Department of Information Technology Salalah College of Technology Chapter No.3 SQL.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Advanced Accounting Information Systems Day 12 Understanding the SQL Language September 21, 2009.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
How to: SQL By: Sam Loch.
SQL Query Getting to the data ……..
Structured Query Language
Chapter 3 Introduction to SQL(3)
Subqueries Schedule: Timing Topic 25 minutes Lecture
Group Functions Lab 6.
SQL FUNDAMENTALS CDSE Days 2018.
Chapter 4 Summary Query.
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Database systems Lecture 3 – SQL + CRUD
Structured Query Language
CSC 453 Database Systems Lecture
Section 4 - Sorting/Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Introduction to SQL Server and the Structure Query Language
Presentation transcript:

CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP

SELECT Statement SELECT [ALL|DISTINCT] column_list FROM tbl_1 [tbl_alias1] [{, tbl_alias2]}…] o Column_name [AS] column_heading

WHERE Clause SELECT select_list [INTO new_tbl] FROM table [WHERE search_condition] [GROUP BY group_by_expression] [HAVING search_condition] [ORDER BY order_expression [ASC|DESC]] o See P115 for more operator.

WHERE Clause (Boolean Operators) SELECT emp_no, project_no FROM works_on WHERE project_no = ‘p2’ AND job = ‘Clerk’ SELECT project_no, emp_no FROM works_on WHERE project_no = ‘p1’ OR project_no = ‘p2’

WHERE Clause (IN and BETWEEN) SELECT * FROM employee WHERE emp_no IN (29346, 28559, 25348) SELECT * FROM employee WHERE emp_no NOT IN (10102, 9031) SELECT project_name, budget FROM project WHERE budget BETWEEN and

WHERE Clause (NULL and NOT NULL) SELECT emp_no, project_no FROM works_on WHERE project_no =‘p2’ AND job IS NULL SELECT emp_no, ISNULL (job, ‘job unknown’) task FROM works_on WHERE project_no = ‘p1’

WHERE Clause (LIKE Operator) Culumn [NOT] LIKE ‘pattern’ o Two wildcard o % = any sequence of zero or more characters. o _ = any single character. SELECT emp_fname, emp_lname, emp_no FROM employee WHERE emp_lname LIKE ‘J%’

Simple Subqueries o There are two types of subqueries o Simple – The inner query is evaluated exactly once o Correlated – The inner query is evaluated each time the system retrieves a new row. SELECT emp_fname, emp_lname FROM employee WHERE dept_no = (SELECT dept_no FROM department WHERE dept_name =‘Research’)

Subqueries and IN Operator SELECT * FROM employee WHERE dept_no IN (SELECT dept_no FROM department WHERE location = ‘Dallas’)

Queries in the FROM Clause o Get the names of all employee with employee numbers greater than or equal to SELECT emp_fname, emp_lname FROM (SELECT * FROM employee WHERE emp_no >= 10000) AS empno_10000

GROUP BY Clause SELECT job FROM works_on GROUP BY job

Aggregate Functions o All aggregate functions can be divided into three groups o Convenient aggregate functions o MIN o MAX o SUM o AVG o COUNT o Statistical aggregate functions o Superaggregates o In SELECT statement, no simple columns can be included except to be used with GROUP BY clause.

Aggregate Functions (Cont.) SELECT MIN (emp_no) min_employee_number FROM employee SELECT emp_no, emp_lname FROM employee WHERE emp_no = (SELECT MIN(emp_no) FROM employee) SELECT job, COUNT(*) job_count FROM works_on GROUP BY job

HAVING Clause HAVING condition SELECT project_no FROM works_on GROUP BY project_no HAVING COUNT(*) < 4

ORDER BY Clause ORDER BY {[col_name | col_number [ASC | DESC ]]}, … SELECT emp_no, emp_fname, emp_lname FROM employee ORDER BY emp_no

Set Operators o UNION select_1 UNION [ALL] select_2 { [UNION [ALL] select_3]}… o INTERSECTION o Shows the record that belong to both tables o DIFFERENCE

Create/Update employee_enh table USE sample SELECT * INTO employee_enh FROM employee ALTER TABLE employee_enh ADD domicile CHAR(25) NULL

Create/Update employee_enh table UPDATE employee_enh set domicile = ‘San Antonio’ WHERE emp_no = OR emp_no = UPDATE employee_enh set domicile = ‘Houston’ WHERE emp_no = OR emp_no = UPDATE employee_enh set domicile = ‘Seattle’ WHERE emp_no = UPDATE employee_enh set domicile = ‘Portland’ WHERE emp_no = 9031 UPDATE employee_enh set domicile = ‘Tacoma’ WHERE emp_no = 2581

UNION USE sample SELECT domicile FROM employee_enh UNION SELECT location FROM department o To use UNION operator, two table must have the same number of columns, and the corresponding columns have the compatible data types. o GROUP BY and HAVING cannot be used with the UNION

INTERSECTION USE sample SELECT emp_no FROM employee WHERE dept_no = ‘d1’ INTERSECT SELECT emp_no FROM works_on WHERE enter_date < ‘ ’

CASE Expressions o Simple CASE Expression o CASE Expression_1 o {WHEN expression_2 THEN result_1} o [ELSE result_n] o END

CASE Expression (2) o Searched CASE expression o CASE o {WHEN condition_1 THEN result_1} o [ELSE result_n] o END

CASE Expression (3) o SELECT project_name, o CASE o WHEN budget > 0 AND budget < THEN 1 o WHEN budget >= AND budget < THEN 2 o ELSE 4 o END budget_weight o FROM project

COMPUTE o To use with MIN, MAX, AVG, SUM, COUNT o SELECT emp_no, project_no, enter_date o FROM works_on o WHERE project_no = ‘p1’ o OR project_no =‘p2’ o COMPUTE MIN(enter_date)

COMPUTE (2) o SELECT emp_no, project_no, enter_date o FROM works_on o WHERE project_no = ‘p1’ o OR project_no =‘p2’ o ORDER BY project_no o COMPUTE MIN(enter_date) BY project_no

Temporary Table o Local temporary table will be removed at the end of the current session o # - #tbl_name o Global temporary table will be dropped at the end of the session that created this table o ##

Temporary Table (2) o CREATE TABLE #project_temp o (project_no CHAR(4) NOT NULL, o Project_name CHAR(25) NOT NULL) o SELECT project_no, project_name o INTO #project_temp1 o FROM project

Computed columns o CREATE TABLE orders o (orderid INT NOT NULL, o Price FLOAT NOT NULL, o Quantity INT NOT NULL, o Orderdate DATETIME NOT NULL, o Total AS price * quantity PERSISTED o Shippeddate AS DATEADD(DAY,7,orderdate))

Exercise o P166 o E.5.1 – E.5.12 o *Note* E.5.3, do only one SELECT o E.5.15 – E.5.17