SQL Cheat Sheet Query By Example. Why Use SQL Over procedural 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. You leave it up to the Database process to decide how best to collect that data and apply your operations. Access Update Query Left Join Example' title='Access Update Query Left Join Example' />In a procedural language, you basically map out step by 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 procedure in 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 too because all the underlining looping iteration is delegated to a database server process that does it in a very low level way and uses IOprocessor more efficiently and knows the current state of the data e. If you were to update say a sales person of all customers in a particular region your procedural way would look something like this. NH then. rssalesperson Mike. 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. Disable Windows Authentication In Iis7 Enable Asp. In this article we will provide some common data questions and processes that SQL is well suited for and SQL solutions to these tasks. Most of these examples are fairly standard ANSI SQL so should work on most relational databases such as IBM DBII, Post. Gre. SQL, My. SQL, Microsoft SQL Server, Oracle, Microsoft Access, SQLite with little change. Some examples involving subselects or complex joins or the more complex updates involving 2 or more tables may not work in less advanced relational databases such as My. SQL, MSAccess or SQLite. These examples are most useful for people already familiar with SQL. We will not go into any detail about how these work and why they work, but leave it up to the reader as an intellectual exercise. List all records from one table that are in another table. What customers have bought from us SELECT DISTINCT customers. FROM customers INNER JOIN orders ON customers. What items are in one table that are not in another table Example What customers have never ordered anything from usSELECT customers. FROM customers LEFT JOIN orders ON customers. WHERE orders. customerid IS NULL. More advanced example using a complex join What customers have not ordered anything from us in the year 2. IN clauseSELECT customers. FROM customers LEFT JOIN orders ON customers. AND yearorders. orderdate 2. WHERE orders. orderid 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, My. SQL support year. Post. Gre. SQL you do datepartyear, orders. SQLite substrorders. If you store the date in form YYYY MM DDOracle EXTRACTYEAR FROM orderdate or tocharorderdate,YYYYNote You can also do the above with an IN clause, but an IN tends to be slower. Same question with an IN clause. SELECT customers. FROM customers WHERE customers. NOT INSELECT customerid FROM orders WHERE yearorders. Fun with Statistics Aggregates. How many customers do we have in Massachusetts and California SELECT customerstate As state, COUNTcustomerid As total. WHERE customerstate INMA, CA. GROUP BY customerstate. What states do we have more than 5 customers SELECT customerstate, COUNTcustomerid As total. GROUP BY customerstate. HAVING COUNTcustomerid 5. How many states do we have customers in SELECT COUNTDISTINCT customerstate AS total. Note the above does not work in Microsoft Access or SQLite they do not support COUNTDISTINCT. Alternative but slower approach for the above for databases that dont support COUNTDISTINCT., but support derived tables. SELECT countcustomerstate FROM SELECT DISTINCT customerstate FROM customers. List in descending order of orders placed customers that have placed more than 5 orders. SELECT customerid, customername, COUNTorderid as total. FROM customers INNER JOIN orders ON customers. GROUP BY customerid, customername. HAVING COUNTorderid 5. ORDER BY COUNTorderid DESC. How do you insert records in a table Value Insert. INSERT INTO customerscustomerid, customername. VALUES1. 23. 45, GIS Experts. Copy data from one table to another table. INSERT INTO customerscustomerid, customername. SELECT cuskey, cusname. FROM jimmyscustomers WHERE customername LIKE B. Creating a new table with a bulk insert from another table. SELECT INTO archivecustomers. FROM jimmyscustomers WHERE active 0. How do you update records in a table Update from values. UPDATE customers. SET customersalesperson Billy WHERE customerstate TX. Update based on information from another table. UPDATE customers. SET rating Good. WHERE orderdate 2. 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. FROM SELECT customerid, countorderid As total. FROM orders GROUP BY customerid As ordersummary. WHERE customers. customerid ordersummary. Please note the update examples involving additional tables do not work in My. SQL, MSAccess, SQLite. MS Access Specific syntax for doing multi table UPDATE joins. UPDATE customers INNER JOIN orders ON customers. SET customers. rating Good My. SQL 5 Specific syntax for doing multi table UPDATE joins. UPDATE customers, orders SET customers. Good WHERE orders. Articles of Interest. Postgre. SQL 8. 3 Cheat Sheet. Summary of new and old Postgre. SQL functions and SQL constructs complete xml query and export, and other new 8. SQLite. If you are looking for a free and lite fairly SQL 9. SQLite has ODBC drivers, PHP 5 already comes with an embedded SQLite driver, there are. NET drivers, freely available GUIs, and this will run on most Oses. All the data is stored in a single. So when you want something lite and dont want to go thru a database server install as you would have to with My. SQL, MSSSQL, Oracle, Postgre. SQL, or dont have admin access to your webserver and you dont need database group user permissions infrastructure, this is very useful. It also makes a nice transport mechanism for relational data as the size the db file is pretty much only limited to what your OS will allow for a file or 2 terabytes which ever is lower. Postgre. SQL Date Functions. Summary of Post. Gresql Date functions in 8. The Future of SQL by Craig Mullins. Provides a very good definition of what set based operations are and why SQL is superior for these tasks over procedural, as well as a brief history of the language. Summarizing data with SQL Structured Query LanguageArticle that defines all the components of an SQL statement for grouping data. We wrote it a couple of years ago, but it is still very applicable today. Procedural Versus Declarative Languages. Provides some useful anlaogies for thinking about the differences between procedural languages and a declarative language such as SQLPostgre. SQL Cheat Sheet. Cheat sheet for common Postgre. SQL tasks such as granting user rights, backing up databases, table maintenance, DDL commands create, alter etc., limit queries.