SQL Code Example

SQL Code Example

CRUD statement

INSERT INTO person (person_id, fname, lname, gender, birth_date)

VALUES (null, 'William','Turner', 'M', '1972-05-27');

UPDATE person SET street = '1225 Tremont St.',

city = 'Boston', state = 'MA', country = 'USA', postal_code = '02138'

WHERE person_id = 1;

DELETE FROM person WHERE person_id = 2;

DROP TABLE favorite_food;

The group by and having Clauses[key point]

Count all the employees in each department and returns the names of those departments having more than two employees:

SELECT d.name, count(e.emp_id) num_employees

FROM department d INNER JOIN employee e

ON d.dept_id = e.dept_id

GROUP BY d.name

HAVING count(e.emp_id) > 2;

The order by Clause

Ascending Versus Descending Sort Order[Asc, Desc]

Filtering


Using Parentheses

Using the not Operator

Condition Types: =, <>, <, >

DELETE FROM account WHERE status = 'CLOSED' AND YEAR(close_date) = 2002;

The between operator(Number, String, Date)

Membership Conditions(in, not in)

Using wildcards(LIKE)

_ Exactly one character

% Any number of characters (including 0)

Using regular expressions

SELECT emp_id, fname, lname FROM employee WHERE lname REGEXP '^[FG]';

Null(IS [not] NULL)

Querying Multiple Tables[Join]

Self-Joins

List every employee’s name along with the name of his or her manager:

SELECT e.fname, e.lname, e_mgr.fname mgr_fname, e_mgr.lname mgr_lname

FROM employee e INNER JOIN employee e_mgr ON e.superior_emp_id = e_mgr.emp_id;

Using the Same Table Twice

SELECT a.account_id, e.emp_id, b_a.name open_branch, b_e.name emp_branch

FROM account a INNER JOIN branch b_a ON a.open_branch_id = b_a.branch_id

INNER JOIN employee e ON a.open_emp_id = e.emp_id

INNER JOIN branch b_e ON e.assigned_branch_id = b_e.branch_id

WHERE a.product_cd = 'CHK';

Using Subqueries As Tables

Non-Equi-Joins

List all possible chess matches:

SELECT e1.fname, e1.lname, 'VS' vs, e2.fname, e2.lname

FROM employee e1 INNER JOIN employee e2 ON e1.emp_id < e2.emp_id

WHERE e1.title = 'Teller' AND e2.title = 'Teller';

Grouping and Aggregates[key point]

SELECT open_emp_id, COUNT(*) how_many FROM account

GROUP BY open_emp_id HAVING COUNT(*) > 4;

Aggregate Functions

Max(), Min(),Avg(), Sum(), Count()

SELECT product_cd, MAX(avail_balance) max_balance,

MIN(avail_balance) min_balance, AVG(avail_balance) avg_balance,

SUM(avail_balance) tot_balance, COUNT(*) num_accts

FROM account GROUP BY product_cd;

Counting Distinct Values

SELECT COUNT(DISTINCT open_emp_id) FROM account;

SELECT product_cd, open_branch_id, SUM(avail_balance) tot_balance

FROM account GROUP BY product_cd, open_branch_id;

Grouping via Expressions

SELECT EXTRACT(YEAR FROM start_date) year, COUNT(*) how_many

FROM employee GROUP BY EXTRACT(YEAR FROM start_date);

Group Filter Conditions[having]

Subqueries

SELECT account_id, product_cd, cust_id, avail_balance

FROM account WHERE account_id = (SELECT MAX(account_id) FROM account);

Subquery Types

Noncorrelated Subqueries

Single-Row, Single-Column Subqueries

This type of subquery is known as a scalar subquery and can appear on either side of a condition using the usual operators (=, <>, <,>, <=, >=).

Multiple-Row, Single-Column Subqueries[in and not in, all, any]

List which employees supervise other employees:

SELECT emp_id, fname, lname, title FROM employee

WHERE emp_id IN (SELECT superior_emp_id FROM employee);

SELECT emp_id, fname, lname, title FROM employee

WHERE emp_id NOT IN (SELECT superior_emp_id

FROM employee WHERE superior_emp_id IS NOT NULL);

Find all employees whose employee IDs are not equal to any of the supervisor employee IDs:

SELECT emp_id, fname, lname, title FROM employee

WHERE emp_id <> ALL (SELECT superior_emp_id

FROM employee WHERE superior_emp_id IS NOT NULL);

Multicolumn Subqueries

Find all accounts whose balances don’t match the sum of the transaction amounts for that account.

Correlated Subqueries

List customers having exactly two accounts

SELECT c.cust_id, c.cust_type_cd, c.city FROM customer c

WHERE 2 = (SELECT COUNT(*) FROM account a WHERE a.cust_id = c.cust_id);

The exists Operator

Find all the accounts for which a transaction was posted on a particular day

SELECT a.account_id, a.product_cd, a.cust_id, a.avail_balance FROM account a

WHERE EXISTS (SELECT 1 FROM transaction t WHERE t.account_id = a.account_id AND t.txn_date = '2008-09-22');

UPDATE account a SET a.last_activity_date =

(SELECT MAX(t.txn_date) FROM transaction t WHERE t.account_id = a.account_id);

WHERE EXISTS (SELECT 1 FROM transaction t WHERE t.account_id = a.account_id);

Remove data from the department table that has no child rows in the employee table:

DELETE FROM department WHERE NOT EXISTS

(SELECT 1 FROM employee WHERE employee.dept_id = department.dept_id);

Subqueries in Filter Conditions

Find the employee responsible for opening the most accounts:

SELECT open_emp_id, COUNT(*) how_many FROM account GROUP BY open_emp_id

HAVING COUNT(*) = (SELECT MAX(emp_cnt.how_many) FROM (SELECT COUNT(*) how_many

FROM account GROUP BY open_emp_id) emp_cnt);

Working with Sets

Set Operators[union, union all, intersect, except, ]

union all operator doesn’t remove duplicates.

SELECT 'IND' type_cd, cust_id, lname name FROM individual

UNION ALL

SELECT 'BUS' type_cd, cust_id, name FROM business;

Conditional Logic

CASE

WHEN C1 THEN E1

WHEN CN THEN EN

[ELSE ED]

END

CASE V0

WHEN V1 THEN E1

WHEN VN THEN EN

[ELSE ED]

END

SELECT

SUM(CASE WHEN EXTRACT(YEAR FROM open_date) = 2010 THEN 1 ELSE 0 END) year_2010,

SUM(CASE WHEN EXTRACT(YEAR FROM open_date) = 2011 THEN 1 ELSE 0 END) year_2011,

FROM account WHERE open_date > '1999-12-31' AND open_date < '2006-01-01';

Find accounts whose account balances don’t agree with the raw data in the transaction table.

SELECT CONCAT('ALERT! : Account #', a.account_id,

' Has Incorrect Balance!')

FROM account a

WHERE (a.avail_balance, a.pending_balance) <>

(SELECT

SUM(CASE

WHEN t.funds_avail_date > CURRENT_TIMESTAMP() THEN 0

WHEN t.txn_type_cd = 'DBT' THEN t.amount * −1

ELSE t.amount

END),

SUM(CASE WHEN t.txn_type_cd = 'DBT' THEN t.amount * −1 ELSE t.amount END)

FROM transaction t

WHERE t.account_id = a.account_id);

Show whether the customer has any checking accounts and whether the customer has any savings accounts:

SELECT c.cust_id, c.fed_id, c.cust_type_cd,

CASE

WHEN EXISTS (SELECT 1 FROM account a

WHERE a.cust_id = c.cust_id

AND a.product_cd = 'CHK') THEN 'Y'

ELSE 'N'

END has_checking,

CASE

WHEN EXISTS (SELECT 1 FROM account a

WHERE a.cust_id = c.cust_id

AND a.product_cd = 'SAV') THEN 'Y'

ELSE 'N'

END has_savings

FROM customer c;

Count the number of accounts for each customer, and then returns either 'None', '1', '2', or '3+':

SELECT c.cust_id, c.fed_id, c.cust_type_cd,

CASE (SELECT COUNT(*) FROM account a

WHERE a.cust_id = c.cust_id)

WHEN 0 THEN 'None'

WHEN 1 THEN '1'

WHEN 2 THEN '2'

ELSE '3+'

END num_accounts

FROM customer c;

Division-by-Zero Errors

SELECT a.cust_id, a.product_cd, a.avail_balance /

CASE

WHEN prod_tots.tot_balance = 0 THEN 1

ELSE prod_tots.tot_balance

END percent_of_total

FROM account a INNER JOIN

(SELECT a.product_cd, SUM(a.avail_balance) tot_balance

FROM account a

GROUP BY a.product_cd) prod_tots

ON a.product_cd = prod_tots.product_cd;

Transactions

Lock Granularities:Table locks, Page locks, Row locks

SHOW TABLE STATUS LIKE 'transaction' \G

START TRANSACTION;

UPDATE product

SET date_retired = CURRENT_TIMESTAMP() WHERE product_cd = 'XYZ';

SAVEPOINT before_close_accounts;

UPDATE account

SET status = 'CLOSED', close_date = CURRENT_TIMESTAMP(),

last_activity_date = CURRENT_TIMESTAMP()

WHERE product_cd = 'XYZ';

ROLLBACK TO SAVEPOINT before_close_accounts;

COMMIT;

Resource

Learning SQL

Post a Comment

Labels

Java (159) Lucene-Solr (112) Interview (61) All (58) J2SE (53) Algorithm (45) Soft Skills (38) Eclipse (33) Code Example (31) Linux (25) JavaScript (23) Spring (22) Windows (22) Web Development (20) Tools (19) Nutch2 (18) Bugs (17) Debug (16) Defects (14) Text Mining (14) J2EE (13) Network (13) Troubleshooting (13) PowerShell (11) Chrome (9) Design (9) How to (9) Learning code (9) Performance (9) Problem Solving (9) UIMA (9) html (9) Http Client (8) Maven (8) Security (8) bat (8) blogger (8) Big Data (7) Continuous Integration (7) Google (7) Guava (7) JSON (7) Shell (7) ANT (6) Coding Skills (6) Database (6) Lesson Learned (6) Programmer Skills (6) Scala (6) Tips (6) css (6) Algorithm Series (5) Cache (5) Dynamic Languages (5) IDE (5) System Design (5) adsense (5) xml (5) AIX (4) Code Quality (4) GAE (4) Git (4) Good Programming Practices (4) Jackson (4) Memory Usage (4) Miscs (4) OpenNLP (4) Project Managment (4) Spark (4) Testing (4) ads (4) regular-expression (4) Android (3) Apache Spark (3) Become a Better You (3) Concurrency (3) Eclipse RCP (3) English (3) Happy Hacking (3) IBM (3) J2SE Knowledge Series (3) JAX-RS (3) Jetty (3) Restful Web Service (3) Script (3) regex (3) seo (3) .Net (2) Android Studio (2) Apache (2) Apache Procrun (2) Architecture (2) Batch (2) Bit Operation (2) Build (2) Building Scalable Web Sites (2) C# (2) C/C++ (2) CSV (2) Career (2) Cassandra (2) Distributed (2) Fiddler (2) Firefox (2) Google Drive (2) Gson (2) How to Interview (2) Html Parser (2) Http (2) Image Tools (2) JQuery (2) Jersey (2) LDAP (2) Life (2) Logging (2) Python (2) Software Issues (2) Storage (2) Text Search (2) xml parser (2) AOP (1) Application Design (1) AspectJ (1) Chrome DevTools (1) Cloud (1) Codility (1) Data Mining (1) Data Structure (1) ExceptionUtils (1) Exif (1) Feature Request (1) FindBugs (1) Greasemonkey (1) HTML5 (1) Httpd (1) I18N (1) IBM Java Thread Dump Analyzer (1) JDK Source Code (1) JDK8 (1) JMX (1) Lazy Developer (1) Mac (1) Machine Learning (1) Mobile (1) My Plan for 2010 (1) Netbeans (1) Notes (1) Operating System (1) Perl (1) Problems (1) Product Architecture (1) Programming Life (1) Quality (1) Redhat (1) Redis (1) Review (1) RxJava (1) Solutions logs (1) Team Management (1) Thread Dump Analyzer (1) Visualization (1) boilerpipe (1) htm (1) ongoing (1) procrun (1) rss (1)

Popular Posts