You are on page 1of 7

(Level 1 Header) STRUCTURED QUERY LANGUAGE (SQL) (Comp: Please insert the following by the side of the next

paragraph: LO3: Describe how to query using Structured Query Language (SQL)) SQL (usually pronounced "Sequel") stands for Structured Query Language and is a computer language designed to query data in a relational database. SQL is based on relational algebra and allows a user to query and update the database. In a database, while queries allow the user to access, read and report on desired data, the responsibility of actually making physical changes to the relational database belongs to the Database Management System (DBMS). The SELECT statement is used to begin a query. The SELECT statement tells the query which columns (or attributes) of a table should be included in the query. A query includes a list of columns to be included in the final result immediately following the SELECT keyword. An asterisk ("*") can also be used to specify that the query should return all columns of the queried tables. SELECT is the most complex statement in SQL, with optional keywords and clauses that include: (1) The FROM clause to the SELECT statement indicates the name of table(s) from which to retrieve data. SQL Example #1: Assume you would like to use a query to find the salesperson for each customer.
Customer Customer# C-1 C-2 C-3 C-4 C-5 Name A/R Amt SP# Bill 345 E-12 Mick 225 E-10 Keith 718 E-10 Charlie 828 E-99 Ron 3,200 E-10

If we use the following SQL commands, we are asking SQL to select the Customer #, Name and SP# attributes from the Customer Table: SELECT Customer#, Name, SP# FROM Customer; We will get the following query result:
Customer# C-1 C-2 C-3 C-4 C-5 Name Bill Mick Keith Charlie Ron SP# E-12 E-10 E-10 E-99 E-10

(2) The WHERE clause states the criteria that must be met to be shown in the query result. There are many search criteria that you can specify for the final result. Search criteria using relational operators, between operator, and like operator are very common in SQL commands. There are six relational operators in SQL. Their definitions are listed below. Well see how to these operators with the provided SQL examples. Relational Operators = != or <> < <= > >= Meaning equal not equal less than less than or equal to greater than greater than or equal to

SQL Example #2: Cash Receipt Remittance Advice# Amount RA-1 1,666 RA-2 10,000 RA-3 72,000 RA-4 32,600 RA-5 1,669

Bank Account# BA-6 BA-7 BA-7 BA-7 BA-6

Date 25-JUL-2011 26-JUL-2011 15-AUG-2011 15-AUG-2011 25-AUG-2011

Customer Number C-2 C-2 C-1 C-5 C-2

Cashier Number E-39 E-39 E-39 E-39 E-39

If we use the following SQL command, we are asking SQL to retrieve all cash receipt information for customer C-2. SELECT * FROM [Cash Receipt] WHERE [Customer Number] = C-2; (Note: The asterisk (*) following the SELECT SQL statement is a wild card indicating all columns should be selected. The brackets are needed following the FROM and WHERE clauses since there are spaces in the table and field names.) We will get the following query result: Remittance Advice# RA-1 RA-2 RA-5 Bank Account# BA-6 BA-7 BA-6 Customer Number C-2 C-2 C-2 Cashier Number E-39 E-39 E-39

Amount 1,666 10,000 1,669

Date 25-JUL-2011 26-JUL-2011 25-AUG-2011

Notice the WHERE command eliminated those rows that did not have Customer Number equal to C-2. SELECT DISTINCT clause selects a column without showing repetitive values. For example, youll be able to retrieve all customers from the Cash Receipt table with the following command: SELECT DISTINCT [Customer Number] FROM [Cash Receipt]; You will get the following query result back: Customer Number C-2 C-1 C-5

Similarly, if you are interested in those entries with amount 10000, you can change the SQL command to: SELECT * FROM [Cash Receipt] WHERE Amount >= 10000; You will get the following query result back: Remittance Advice# RA-2 RA-3 RA-4 Bank Amount Account# 10,000 BA-7 72,000 BA-7 32,600 BA-7 Customer Date Number 26-JUL-2011 C-2 15-AUG-2011 C-1 25-AUG-2011 C-5 Cashier Number E-39 E-39 E-39

Range Operator (BETWEEN): The BETWEEN operator can be used to specify the end points of a range. Possible criteria can be WHERE Amount BETWEEN 1000 AND 2000 or WHERE Date BETWEEN 01-JAN2011 AND 31-DEC-2011 . Assuming that you are interested in those cash receipt entries in July, then you can issue the following SQL command to retrieve those entries. SELECT * FROM [Cash Receipt] WHERE Date BETWEEN 01-JUL-2011 AND 31-JUL-2011;

The query result will be: Remittance Bank Advice# Amount Account# Date RA-1 1,666 BA-6 25-JUL-2011 RA-2 10,000 BA-7 26-JUL-2011 Membership Operator (IN): Another common search condition is the membership operator (IN). This operator allows you to test whether a data value matches the specified target values. For example, your SQL command can be: SELECT * FROM [Cash Receipt] WHERE *Customer Number+ IN (C-1, C-2); Pattern Matching Operator (LIKE): The last example that we want to present in the search criteria is using a pattern matching operator (LIKE). The wildcard character (%) is used to match any sequence of zero, one, or more characters. For example, suppose you know that a customers name start with C then you can issue the query command in SQL example #1 as follows. SELECT * FROM Customer WHERE Name LIKE C%; The query result will be: Customer# Name C-4 Charlie Customer Number C-2 C-2 Cashier Number E-39 E-39

SP# E-199

(3) The ORDER BY clause identifies which columns are used to sort the resulting data. If there is no ORDER BY clause, the order of rows returned by an SQL query will not be defined. SQL Example #3: Continuing example #2, if we had used the following SQL commands instead, the amount of cash receipt would be ordered in ascending amount (ASC) or descending amount (DESC): SELECT * FROM [Cash Receipt] WHERE [Customer Number] = C-2 ORDER BY Amount ASC;
Cash Receipt Remittance Advice# Amount RA-1 1,666 RA-5 1,669 RA-2 10,000

Bank Account# BA-6 BA-6 BA-7

Date 25 July 25 Aug 26 July

Customer Number C-2 C-2 C-2

Cashier Number E-39 E-39 E-39

SQL language provides several convenient aggregate functions to be used in SQL commands. These aggregate functions include AVG, SUM, MAX, MIN, and COUNT. Their definitions are as follows: AVG(X): gives the average of column X. SUM(X): gives the summation of all rows that satisfy the selection criteria for column X. MAX(X): gives the maximum value of column X. MIN(X): gives the minimum value of column X. COUNT(): gives the number of rows that satisfy the given condition.

Ex1: To query the total amount and average amount from the Cash Receipt table, use SELECT SUM(Amount), AVG(Amount) FROM [Cash Receipt]; Ex2: To query the largest amount entry from the Cash Receipt table, use SELECT MAX(Amount) FROM [Cash Receipt]; Ex3: To query the total amount occurred in July from the Cash Receipt table, use SELECT SUM(Amount) FROM [Cash Receipt] WHERE Date BETWEEN 01-JUL-2012 AND 31-JUL-2012; GROUP BY: The GROUP BY operator is used with aggregate functions on the query results based on one or more columns. For example, you can use the following command to know the remittance total amount for customers C-1 and C-2: SELECT [Customer Number], SUM(Amount) FROM [Cash Receipt] WHERE [Customer Number+ IN (C-1, C-2) GROUP BY [Customer Number]; The query results will be: Cash Receipt Customer Number SUM(Amount) C-1 72,000 C-2 13,335

INSERT INTO: The INSERT INTO operator inserts data into a SQL table. For example, you can insert a row into Cash Receipt table with the following SQL command: INSERT INTO [Cash Receipt] VALUES (RA-6, 5000, BA-7, 28-AUG-2011, C-2, E-39);

After insertion, the Cash Receipt table will have one more row and the result of execution will look like this: Cash Receipt Remittance Advice# Amount RA-1 1,666 RA-2 10,000 RA-3 72,000 RA-4 32,600 RA-5 1,669 RA-6 5,000

Bank Account# BA-6 BA-7 BA-7 BA-7 BA-6 BA-7

Date 25-JUL-2011 26-JUL-2011 15-AUG-2011 15-AUG-2011 25-AUG-2011 28-AUG-2011

Customer Number C-2 C-2 C-1 C-5 C-2 C-2

Cashier Number E-39 E-39 E-39 E-39 E-39 E-39

UPDATE: The UPDATE operator is for updating data in a SQL table. For example, you can use the following SQL UPDATE command to change the Amount value from 5000 to 6000 for the inserted entry: UPDATE [Cash Receipt] SET Amount = 6000 WHERE *Remittance Advice#+ = RA-6;

DELETE FROM: The DELETE FROM operator deletes data from a SQL table. For example, to delete the above inserted entry, you can use the following SQL DELETE FROM statement: DELETE FROM [Cash Receipt] WHERE *Remittance Advice#+ = RA-6;

After SQL DELETE command, the record with Remittance Advice# RA-6 will be deleted from Cash Receipt table and the table will be the same as the original one.

SQL create commands, join commands, union commands, and many other SQL query commands that are beyond the scope of this textbook. See for example, http://www.w3schools.com/sql / for a list of popular SQL query commands.

You might also like