Structured Query Language NEU – CCIS – CSU430 Tony.

Slides:



Advertisements
Similar presentations
Structured Query Language (SQL)
Advertisements

Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.
Alternative Database topology: The star schema
Transact-SQL. 1. Declare float = 10 select * from customers where discnt
COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data.
1 SQL - Select Join / Outer Join Sub queries Join Join Outer join Left outer join Right outer join.
CS411 Database Systems Kazuhiro Minami 06: SQL. Join Expressions.
Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.
Introduction to Relational Database ISYS 464. Introduction to Relational Model Data is logically structured within relations. Each relation is a table.
Querying a Database Using the Select Query Window
Introduction to Oracle9i: SQL1 SQL Group Functions.
--The SQL Query Language DML--1 LIKE  LIKE allows to select character strings which have some element in common by using wild cards:  Wild cards:  “%”
Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.
Querying Database. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy Calculated.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Interpreting SQL Code. SQL (The language used to query a database) S is used to specify the you want to include. F is used to specify the the selected.
Microsoft Access 2010 Chapter 7 Using SQL.
Computer Science 101 Web Access to Databases SQL – Extended Form.
SQL – Logical Operators and aggregation Chapter 3.2 V3.0 Napier University Dr Gordon Russell.
Slide 1 Chapter 2 Introduction to Structured Query Language (SQL) ‏
Concepts of Database Management, Fifth Edition
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
Chapter 3 Single-Table Queries
Data Manipulation 11 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and other programming.
IM433-Industrial Data Systems Management Lecture 5: SQL.
Database Queries. Queries Queries are questions used to retrieve information from a database. Contain criteria to specify the records and fields to be.
1. SQL Header of tables: –Head(Custoemrs) = (cid, cname, city, discnt) –Head(Orders) = (ordno, month, cid, pid, qty, dollars) –Head(Products) = (pid, pname,
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Using Special Operators (LIKE and IN)
Querying a Database - A question or an inquiry (dictionary.com) - WHAT ARE WE ASKING QUESTIONS ABOUT? THE DATA - BY ASKING QUESTIONS OF THE DATA WE OBTAIN?
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Querying Database ISYS 363.
Structured Query Language SQL Unit 2 An Introduction to Organizing and Retrieving Data with SQL.
G042 - Lecture 12 Using Local Databases Mr C Johnston ICT Teacher
Introduction to Query Language and SQL. Basic Query Language Operators Selection Projection Join Aggregates –Sum, Count, Max, Min, Avg SubTotal Calculated.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
1 SQL Insert Update Delete Create table Alter table.
1 2 Concepts of Database Management, 4 th Edition, Pratt & Adamski Chapter 2 The Relational Model 1: Introduction, QBE, and Relational Algebra.
Structured Query Language SQL Unit 4 Solving Problems with SQL.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Hassan Tariq INTRODUCTION TO SQL What is SQL? –When a user wants to get some information from a database file, he can issue a query. – A query is a user–request.
Lecture Access – Queries. What’s a Query? A question you ask a database –ie: “Who are my Stockton customers?” –ie: “How much did Bob sell on the 14th?”
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.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Computer Science & Engineering 2111 Inner Joins and Advanced Queries 1CSE 2111 Lecture-Advanced Queries.
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.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Queries.
The Database Exercises Fall, 2009.
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Structured Query Language
Database Queries.
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Querying Database ISYS 363.
Introduction to SQL Wenhao Zhang October 5, 2018.
SQL – Entire Select.
Aggregations Various Aggregation Functions GROUP BY HAVING.
Chapter 4 Summary Query.
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Access: SQL Participation Project
Query Functions.
Projecting output in MySql
Joins and other advanced Queries
LINQ to SQL Part 3.
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

Structured Query Language NEU – CCIS – CSU430 Tony

2 Content Select queries on one table Select queries on multiple tables Manipulate data and table structure

3 0. References CAPS database on textbook P

4 1. Select queries on one table Simple queries Distinct Aggregate functions Sorting query results Like\Between … And Group Alias

5 1.1 Simple queries List all records in products select * from products List all records in records in customers select * from customers

6 1.1 Simple queries (cont) List all product names in the product table select pname from products product[pname] (Projection) ?? List all customer names in the customers table select cname from customers customers[cname] (Projection)

7 1.1 Simple query (cont) List all customer records in Dallas select * from customers where city = 'Dallas‘ customers where city = ‘Dallas` List all order records costs more than 800 select * from orders where dollars > 800 orders where dollars > 800

8 1.1 Simple query (cont) List all customer names in Dallas select cname from customers where city = 'Dallas‘ List the order number, quantity of orders cost more than 800 select ordno, qty from orders where dollars > 800

9 1.2 Distinct List all customer names in the customers table select cname from customers customers[cname] (Not Projection) select distinct cname from customers customers[cname] (Projection)

Distinct (cont) List all distinct agent names select aname from agents select distinct aname from agents

Aggregate functions List the total cost of all orders select sum(dollars) from orders List the total cost of client c001 select sum(dollars) from orders where cid = 'c001'

Aggregate functions (cont) List the average discount of all customers select avg(discnt) from customers List the average discount of customers in Duluth area select avg(discnt) from customers where city = 'Duluth'

Aggregate functions (cont) Most frequent-used aggregate functions Sum Avg Count Min Max … see online documents

Sort query results List the product names and prices in price ascending order select pname, price from products order by price List the product names and prices in price descending order select pname, price from products order by price desc

Like \ Between … And Find all customers in the cities start with letter “D”. select * from customers where city like 'D%‘ Find all products whose name end with letter “r”. select * from products where pname like '%r‘ Find all the agents whose names contain letter “r”. select * from agents where aname like '%r%'

Like \ Between … And (cont) List all orders that cost between 500 and 1000 select * from orders where dollars between 500 and 1000 List all customers whose discount between 5 and 10 select * from customers where discnt between 5 and 10

Group Find the total expense of each client Straightforward solutions: Select sum(dollars) from orders where cid = ‘c001’ Select sum(dollars) from orders where cid = ‘c002’ ……

Group (cont) Find the total expense of each client Select cid,sum(dollars) from orders group by cid

Group (cont) Find total sales amount of each agent Select aid,sum(dollars) from orders group by aid

Group (cont) Find total sales amount of each agent whose total sales amount is more than 1000 dollars. Select aid,sum(dollars) from orders group by aid where sum(dollars) > 1000 Select aid,sum(dollars) from orders group by aid having sum(dollars) > 1000

Group (cont) Find total expense of each client whose total expense is more than 1000 dollars. Select cid,sum(dollars) from orders group by cid having sum(dollars) > 1000

Group (cont) Find the total expense of each client on each product Select pid,cid,sum(dollars) from orders group by cid Fields cannot have multi-values attributes or have any internal structures (First Normal Form Rule).

Group (cont) Find the total expense of each client on each product Select pid,cid,sum(dollars) from orders group by pid,cid

Alias and calculated field Find total expense of each client whose total expense is more than 1000 dollars Select cid as client_id, sum(dollars) as total_cost_per_client from orders group by cid having sum(dollars) > 1000

Alias (cont) Find all orders more expensive than 1000 dollars. Select * from orders o where o.dollars > 1000