Upload
sagalog
View
219
Download
0
Embed Size (px)
Citation preview
7/30/2019 SQL Cheat Sheet_ Query by Example
1/51/5www.paragoncorporation.com/ArticleDetail.aspx?Art icleID=27
Services Rates
Team Customers
Contact Us Papers
Links PostGIS in Action
Store
Back to Papers and ArticlesSQL Cheat Sheet: Query By Example
Copyright 2005 Paragon Corporation ( July 02, 2005)
Structured Query Language (SQL) is a set-based language as opposed to a procedural language.It is the defacto language of relational databases.
The difference between a set-based language vs. a procedural language is that in a set-basedlanguage you define what set of data you want or want to operate on and the atomic operation toapply to each element of the set. You leave it up to the Database process to decide how best tocollect that data and apply your operations. In a procedural language, you basically map out stepby step loop by loop how you collect and update that data.
There are two main reasons why SQL is often better to use than procedural code.
It is often much shorter to write - you can do an update or summary procedurein one line of code that would take you several lines of procedural.For set-based problems - SQL is much faster processor-wise and IO wise toobecause all the underlining looping iteration is delegated to a database server
process that does it in a very low level way and uses IO/processor moreefficiently and knows the current state of the data - e.g. what other processesare asking for the data.
Example SQL vs. Procedural
If you were to update say a sales person of all customers in a particular region -your procedural way would look something like this
do until eof
if rs("state") = "NH" then
rs("salesperson") = "Mike"
end if
rs.next
loop
The SQL way would be:UPDATE customers SET salesperson = "Mike" WHERE state = "NH"
If you had say 2 or 3 tables you need to check, your procedural quickly becomes
difficult to manage as you pile on nested loop after loop.
In this article we will provide some common data questions and processes that SQLis well suited for and SQL solutions to these tasks. Most of these examples arefairly standard ANSI-SQL so should work on most relational databases such as IBMDBII, PostGreSQL, MySQL, Microsoft SQL Server, Oracle, Microsoft Access, SQLite
Why Use SQL Over procedural?
7/30/2019 SQL Cheat Sheet_ Query by Example
2/52/5www.paragoncorporation.com/ArticleDetail.aspx?Art icleID=27
with little change. Some examples involving subselects or complex joins or the morecomplex updates involving 2 or more tables may not work in less advancedrelational databases such as MySQL, MSAccess or SQLite. These examples are mostuseful for people already familiar with SQL. We will not go into any detail abouthow these work and why they work, but leave it up to the reader as an intellectualexercise.
What customers have bought from us?
SELECT DISTINCT customers.customer_id, customers.customer_name
FROM customers INNER JOIN orders ON customers.customer_id = orders.customer_id
Example: What customers have never ordered anything from us?
SELECT customers.* FROM customers LEFT JOIN orders ONcustomers.customer_id = orders.customer_id WHERE orders.customer_id ISNULL
More advanced example using a complex join: What customers have notordered anything from us in the year 2004 - this one may not work in some
lower relational databases (may have to use an IN clause)SELECT customers.* FROM customers LEFT JOIN orders ON(customers.customer_id = orders.customer_id ANDyear(orders.order_date) = 2004) WHERE orders.order_id IS NULL
Please note that year is not an ANSI-SQL function and that many databases do not support it, but
have alternative ways of doing the same thing.
SQL Server, MS Access, MySQL support year().PostGreSQL you do date_part('year', orders.order_date)
SQLite - substr(orders.order_date,1,4) - If you store the date in form YYYY-MM-DDOracle - EXTRACT(YEAR FROM order_date) or to_char(order_date,'YYYY')
Note: You can also do the above with an IN clause, but an IN tends to be slowerSame question with an IN clauseSELECT customers.* FROM customers WHERE customers.customer_id NOTIN(SELECT customer_id FROM orders WHERE year(orders.order_date) =
2004)
How many customers do we have in Massachusetts and California?SELECT customer_state As state, COUNT(customer_id) As total FROMcustomers WHERE customer_state IN('MA', 'CA') GROUP BY customer_state
What states do we have more than 5 customers?
SELECT customer_state, COUNT(customer_id) As total
List all records from one table that are in another table
What items are in one table that are not in another table?
Fun with Statistics - Aggregates
7/30/2019 SQL Cheat Sheet_ Query by Example
3/53/5www.paragoncorporation.com/ArticleDetail.aspx?Art icleID=27
FROM customers
GROUP BY customer_state
HAVING COUNT(customer_id) > 5
How many states do we have customers in?
SELECT COUNT(DISTINCT customer_state) AS total
FROM customers
Note the above does not work in Microsoft Access or SQLite - they do not support COUNT(DISTINCT ..)
Alternative but slower approach for the above - for databases that don'tsupport COUNT(DISTINCT ..), but support derived tables
SELECT count(customer_state) FROM (SELECT DISTINCT customer_state FROM customers);
List in descending order of orders placed customers that have placed more than5 orders
SELECT customer_id, customer_name, COUNT(order_id) as total
FROM customers INNER JOIN orders ON customers.customer_id = orders.customer_id
GROUP BY customer_id, customer_name
HAVING COUNT(order_id) > 5
ORDER BY COUNT(order_id) DESC
Value Insert
INSERT INTO customers(customer_id, customer_name)
VALUES('12345', 'GIS Experts')
Copy data from one table to another table
INSERT INTO customers(customer_id, customer_name)
SELECT cus_key, cus_name
FROM jimmyscustomers WHERE customer_name LIKE 'B%'
Creating a new table with a bulk insert from another table
SELECT * INTO archivecustomers
FROM jimmyscustomers WHERE active = 0
Update from values
UPDATE customers SET customer_salesperson = 'Billy' WHEREcustomer_state = 'TX'
Update based on information from another tableUPDATE customers SET rating = 'Good' FROM orders WHERE orderdate >'2005-01-01' and orders.customer_id = customers.customer_id
How do you insert records in a table?
How do you update records in a table?
7/30/2019 SQL Cheat Sheet_ Query by Example
4/54/5www.paragoncorporation.com/ArticleDetail.aspx?Art icleID=27
Please note the date format varies depending on the database you are using and what date format you
have it set to.
Update based on information from a derived table
UPDATE customers
SET totalorders = ordersummary.total
FROM (SELECT customer_id, count(order_id) As total
FROM orders GROUP BY customer_id) As ordersummary
WHERE customers.customer_id = ordersummary.customer_id
Please note the update examples involving additional tables do not work in MySQL, MSAccess, SQLite.
MS Access Specific syntax for doing multi-table UPDATE joinsUPDATE customers INNER JOIN orders ON customers.customer_id =orders.customer_id SET customers.rating = 'Good'
MySQL 5 Specific syntax for doing multi-table UPDATE joinsUPDATE customers, orders SET customers.rating = 'Good' WHEREorders.customer_id = customers.customer_id
Articles of Interest
PostgreSQL 8.3Cheat Sheet
Summary of new and old PostgreSQL functions and SQL constructscomplete xml query and export, and other new 8.3 features, withexamples.
SQLite If you are looking for a free and lite fairly SQL-92 compliantrelational database, look no further. SQLite has ODBC drivers, PHP 5already comes with an embedded SQLite driver, there are .NETdrivers, freely available GUIs , and this will run on most Oses. All thedata is stored in a single .db file so if you have a writeable folderand the drivers, thats all you need. So when you want somethinglite and don't want to go thru a database server install as you wouldhave to with MySQL, MSSSQL, Oracle, PostgreSQL, or don't have
admin access to your webserver and you don't need database groupuser permissions infrastructure, this is very useful. It also makes anice transport mechanism for relational data as the size the db file ispretty much only limited to what your OS will allow for a file (or 2terabytes which ever is lower).
PostgreSQLDate Functions
Summary of PostGresql Date functions in 8.0 version
The Future ofSQL by CraigMullins
Provides a very good definition of what set-based operations are andwhy SQL is superior for these tasks over procedural, as well as abrief history of the language.
Summarizingdata with SQL(StructuredQueryLanguage)
Article that defines all the components of an SQL statement forgrouping data. We wrote it a couple of years ago, but it is still veryapplicable today.
ProceduralVersusDeclarativeLanguages
Provides some useful anlaogies for thinking about the differencesbetween procedural languages and a declarative language such asSQL
PostgreSQLCheat Sheet
Cheat sheet for common PostgreSQL tasks such as granting userrights, backing up databases, table maintenance, DDL commands
(create, alter etc.), limit queriesMySQL CheatSheet
Covers MySQL datatypes, standard queries, functions
Comparison ofdifferent SQL
This is a great summary of the different offerings of Standard SQL,PostGreSQL, DB2, MSSQL, MySQL, and Oracle. It demonstrates by
7/30/2019 SQL Cheat Sheet_ Query by Example
5/55/5www.paragoncorporation.com/ArticleDetail.aspx?Art icleID=27
Services Rates Papers Staff Links Contact Us
implementations clear example how each conforms or deviates from the ANSI SQLStandards with join syntax, limit syntaxx, views, inserts, boolean,handling of NULLS
SQL Cookbook
Anthony Mo li naro
New $24.49
Best $17.97
Learning SQL
Alan Beaul ieu
New $22.31
Best $13.49
Del.icio.us | Reddit | Digg | BlinkList | Furl Spurl | Simpy
Back to Papers and Articles
Last rejuvenated: 2/24/2013 6:32:35 AM 2013 Paragon Corporation All Rights Reserved