Sunteți pe pagina 1din 7

Aggregates and GROUP BY

Some functions are aggregate functions Combines data from multiple rows COUNT, AVG Non-aggregate columns need to be grouped SELECT city, count(*) FROM patient GROUP BY city; You can refer to existing tables, columns, expressions via aliases Syntax is "AS new_name" Example: SELECT p.f_name || p.l_name AS NAME FROM PATIENT P;
SELECT: List the column(s) from table(s) to be included in the result table FROM: Identifies table(s) from which column(s) will be drawn to appear in the result. Incorporates tables/views needed to 'join' tables to process the query WHERE: Conditional parameters for row selection within a single table or across tables/views in join operations

Aliases

Basic SELECT concepts

BETWEEN and IN

BETWEEN implements a range of values SELECT product_name FROM products WHERE unit_price BETWEEN 125 and 150; Inclusive range IN implements set matching SELECT f_name, l_name FROM patient WHERE city IN ('Milwaukee', 'West Allis', 'Whitefish Bay');

Combining Expressions

AND/OR/NOT Used to combine multiple expressions together Example: SELECT f_name, l_name FROM PATIENT WHERE l_name LIKE 'Smith' AND city NOT LIKE 'Milwaukee';

Comparison Operators

Allows the comparison of two data values Typical comparison operators = Equal >Greater than < Less than >= Greater than or equal to <= Less than or equal to <> Not equal to != Not equal to

DDL

Building, modifying, and removing base tables Defining dynamic views Constructing materialized views Creating data integrity constraints Create indexes

Delete Statement Notation

DELETE FROM table_name WHERE conditional_expression; DELETE FROM table_name; Queries removes duplicate rows of data from result set SELECT DISTINCT customer_name FROM ORDER; Commands used to perform CRUD operations These functions may occur within a single table or across several tables

DISTINCT

DML

DML

INSERT UPDATE DELETE SELECT


You can also perform calculations and manipulations of data in SQL May use mathematical expressions or stored functions Example: SELECT COUNT(*) FROM PATIENT Some stored procedures: COUNT, MAX, MIN, SUM, AVG

Expressions

HAVING

Like a WHERE statement, but for GROUP BY results SELECT city, count() FROM patient GROUP BY city HAVING count() > 500;
SELECT f_name, l_name, med_desc FROM patient p, medication m WHERE p.patient_id = m.patient_id; Do patients that don't have medications get reported?

Inner Join

INSERT INTO table_name VALUES (value_list_for_all_columns);

Insert Statement Notation

INSERT INTO table_name (column_list) VALUES (value_list_for_column_list); INSERT INTO table_name SELECT column_list FROM table_name;

Joins

Inner join (aka equi-join) - Only returns results where the common columns (primary and foreign key) between the two tables are equal Outer join - All results are returned for one of the tables. For the other table, null values are reported if no matching value in the common column is found

LIKE

The LIKE comparator is specific to text values Represent equality for text Often used with wildcards to perform matching Example: SELECT l_name FROM patient WHERE l_name LIKE '*smith'; Value is case-sensitive

Multiple Tables and Joins

Joins allows SQL to use data across multiple tables Remember concept of denormalization? Relies heavily on referential integrity and primary and foreign key relationships Often need to use aliases

ORDER BY

Used to sort query results May be descending or ascending order SELECT f_name, l_name FROM PATIENT ORDER BY l_name ASC, f_name DESC;

Outer Join

No standard syntax SELECT f_name, l_name, med_desc FROM patient p LEFT OUTER JOIN medication m ON p.patient_id = m.patient_id; Do patients that don't have medications get reported?

Select Statement Notation

SELECT [ALL DISTINCT] column_list FROM table_list WHERE conditional_expresstion GROUP BY grouping_column_list HAVING conditional_expression ORDER BY order_column_list;
Structured Query Language First used in System R by IBM This is a standard language and not proprietary to RDBMS products ANSI and ISO define the standard Standard first published in 1986 (SQL/86) Two major standards today are SQL-92 and SQL99

SQL

SQL Advantages:

Reduced training costs Productivity Portability Longevity Reduce dependence on single vendor Cross-system communication
Data Manipulation Language (DML) CRUD Data Definition Language (DDL) Used to create, modify, and remove tables Data Control Language (DCL) Assist the database administrator (DBA) in controlling user privileges and transactions in the database Our focus is on DML

SQL Command Categories

SQL Disadvantages

Can reduce creativity and innovation Difficult to change (vested interest) Using features from specific vendors reduce benefits of the standard Potential to consume excessive computing resources if you are not careful Potential for damage to database if you are not careful

Subqueries - Correlated

The inner query is performed for each outer query row The inner query references some value from the outer query Example - employees with above average education for their department SELECT EMPNO, LASTNAME, WORKDEPT, EDLEVEL FROM CORPDATA.EMPLOYEE X WHERE EDLEVEL > (SELECT AVG(EDLEVEL) FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = X.WORKDEPT)

Subqueries - Inline views

The use of a subquery in the FROM clause Similar to a dynamic view Uses an alias SELECT columns FROM (subquery) alias WHERE expression
Use of one query as part of a conditional expression in the WHERE or HAVING predicates The query in the condition is known as the "inner" query and the main query as the "outer" query SELECT f_name, l_name FROM patient WHERE city_code IN (SELECT city_code FROM city WHERE state_code LIKE '55');

Subqueries - Nested

The SQL Environment

Everything is contained in a schema Schema is a collection of related: Tables Views Domains Constraints Roles Triggers and other items

Update Statement Notation

UPDATE table_name SET column_name = value [, column_name = value] WHERE conditional_expression

What do you get from SELECT?

The SELECT statement is used to retrieve data from the database. Data is returned as a result table (aka set of rows or result set) Often denormalized data is returned

Wildcards

The asterisk (*) in SQL indicates the selection of all columns in a SELECT statements The percent symbol (%) is used to match any number of characters in a text value The underscore (_) represents only one character place

S-ar putea să vă placă și