Delphi Getting Started With SQL Part 1 PDF
Delphi Getting Started With SQL Part 1 PDF
SELECT <column(s)>
FROM <table(s)>;
LAST_NAME MONTHLY_SALARY
The criteria defined by a WHERE ==================== ======================
clause is usually referred to as the
Nelson 8825
search condition. The SELECT state- Young 8125
ment returns only those rows that Lambert 8562.5
Johnson 5386.25
meet the search condition. WHERE Forest 6255
supports a variety of conditional Weston 7191.078125
operators and allows multiple
criteria to be logically combined
➤ Figure 3 (partial listing of rows)
using AND and OR. Some of the
common operations that can be
performed are listed in Figure 5. SELECT * FROM employee WHERE dept_no = 621;
Many SQL databases offer
additional operations. EMP_NO FIRST_NAME LAST_NAME PHONE_EXT HIRE_DATE
====== =============== ==================== ========= ===========
The following examples
illustrate some of the expressions 4 Bruce Young 233 28-DEC-1988
45 Ashok Ramanathan 209 1-AUG-1991
you can use to formulate search 83 Dana Bishop 290 1-JUN-1992
conditions. They are all valid 138 T.J. Green 218 1-NOV-1993
search conditions for the employee
table, so feel free to try them out in
➤ Figure 4 (partial listing of columns)
ISQL before continuing (try SELECT
* FROM employee using each of the
following WHERE clauses).
Operator Meaning
WHERE job_grade IN (1,3,4) AND
job_country = ’USA’ = Equal to
<> Not equal to
WHERE salary / 12 > 10000 > Greater than
< Less than
WHERE phone_ext IS NULL >= Greater than or equal to
<= Less than or equal to
WHERE hire_date BETWEEN BETWEEN x AND y Range: greater than or equal to <x> and
’1-1-90’ AND ’12-31-90’ less than or equal to <y>
IS NULL Contains null value
WHERE UPPER(first_name) IS NOT NULL Contains non-null value
LIKE ’ROBERT%’ IN (x,y,...) Value found in a list
NOT IN (x,y,...) Value not found in a list
It should be noted that the columns LIKE Matches a wildcard pattern
used in the WHERE clause do not
have to be part of the select list;
➤ Figure 5 Common SQL operators
they only have to be available in
the table defined in the FROM clause
of the SELECT statement. Suppose you needed to look up a characters (if any) preceded or
customer and all you could followed the word.
Wildcarding remember was that they were
You can use wildcards to select on located on Newbury Street or Sorting Rows
a character column matching a Avenue or Newbury something. You can sort the rows returned by
given pattern (amazingly enough, You could use the following SQL a SELECT statement by including an
this is sometimes referred to as statement: ORDER BY clause. ORDER BY simply
pattern matching). names the columns you want to
SQL supports two wildcard SELECT customer, address_line1 sort on. Suppose you wanted a list
characters: % is used to match any FROM customer of sales orders sorted by the
number of characters (including WHERE address_line1 amount of the order (see Figure 7
zero), and _ is used to match LIKE ’%Newbury%’; for the output):
exactly one character. You must
use the LIKE operator to use wild- As Figure 6 shows, this select finds SELECT cust_no, order_status,
cards in a character search, other- all rows containing the word total_value
wise the wildcards are taken Newbury anywhere in the column FROM sales
literally. address_line1, regardless of what ORDER BY total_value;
Here the 2 in the ORDER BY means have to combine this information combine the rows such that
“order by the second column in the somehow into a single report. This dept_no in the employee table
select list”; that is, by the is where the concept of a “join” matches dept_no in the department
computed column salary / 12. comes into the picture. table” (in this case the department
If you use both a WHERE clause and A join can occur between two or table also happens to contain a
an ORDER BY clause in the same more tables where each pair of column called department). Take a
select statement, the WHERE clause tables can be linked by a common look at the results shown in Figure
must appear before the ORDER BY field. 9. The result of a join select is
clause, as in the following example: For example, departments are indistinguishable from a single
identified by the dept_no column in table select.
SELECT * FROM employee both the employee table and The WHERE clause defines the
WHERE dept_no = 621 department table, so this column association between the two
ORDER BY phone_ext; can be used to link the two tables tables. The linking columns are not
in a join. In SQL you can use the required to have the same name,
Selecting From WHERE clause to specify the link field but they must be compatible data
Multiple Tables (Joins) for a join between two tables. The types. As an alternative to the WHERE
One of the most powerful features select statement below produces clause, you can also define a join in
of the SELECT statement (and of SQL the employee roster we want: the FROM clause as follows:
itself) is the ease with which you
can combine data from multiple SELECT full_name, department SELECT full_name, department
tables into one informative view. FROM employee, department FROM employee JOIN department
For example, suppose you want WHERE employee.dept_no = ON employee.dept_no =
a roster of all employees by department.dept_no department.dept_no
department. Employee names are ORDER BY department; ORDER BY department;
stored in the employee table and
department names are stored in This means “show the selected You can join more than two tables
the department table, so you’ll columns from the given tables and by simply ANDing the join
tables:
➤ Figure 9 (partial listing of rows)
SELECT full_name, proj_id,
proj_name
FROM employee,
SELECT full_name, proj_id, proj_name
employee_project, project
FROM employee, employee_project, project
WHERE employee.emp_no = WHERE employee.emp_no = employee_project.emp_no AND
employee_project.emp_no employee_project.proj_id = project.proj_id
ORDER BY full_name;
AND
employee_project.proj_id = FULL_NAME PROJ_ID PROJ_NAME
===================================== ======= ====================
project.proj_id
ORDER BY full_name; Baldwin, Janet MKTPR Marketing project 3
Bender, Oliver H. MKTPR Marketing project 3
Bishop, Dana VBASE Video Database
You can see the results in Figure 10. Burbank, Jennifer M. VBASE Video Database
One improvement we could Burbank, Jennifer M. MAPDB MapBrowser port
Fisher, Pete GUIDE AutoMap
make is to reduce the bulk of this Fisher, Pete DGPII DigiPizza
statement a little by assigning
aliases to the tables just as we
➤ Figure 10 (partial listing of rows)
assigned aliases to columns
previously. We do this in the same
fashion by following the actual Committing Your Work (this is yet another boon of SQL by
table name with its alias, however One of the key characteristics of helping preserve data integrity
we do not use the AS keyword in SQL is that when you add or modify through automatic means).
between. We’re going to redefine data in the SQL table, the changes
the employee, employee_project and are not permanently recorded in Adding New Rows
project tables to have the aliases a, the table and other users of the We use the INSERT statement to add
b and c respectively. So our final database will not see them, until a new row to a table. INSERT expects
select statement now looks like: you commit them. In this way you us to enumerate the values of each
can work with the data as much as column in the table for the new
SELECT full_name, proj_id, you like until you get it just the way row.
proj_name you want it and then commit it For example, the country table
FROM employee a, permanently to the database. identifies the currency used in a
employee_project b, In ISQL you commit your particular country and contains
project c changes by selecting Commit Work two columns: country and
WHERE a.emp_no = b.emp_no from the File menu. If you want to currency. To add a new country row
AND b.proj_id = c.proj_id undo your modifications, you can in this table we would use:
ORDER BY full_name; select Rollback Work from the File
menu. This will rollback all the INSERT INTO country
Summing Up SELECT changes you’ve made since the last VALUES (’SteveLand’,
SELECT is where most of the power time you committed your work. ’Twinkies’);
of SQL lies. There are even more Think of this as a refresh of the data
clauses and functionality to SELECT you’re working with. In this case Twinkies are the form
than were covered here, so check Be careful if you decide to of currency in SteveLand. Try
your manual. This was meant just experiment with these data entering this statement into ISQL
to show you the basic nuts-and- modification statements outside and then select all the rows from
bolts needed to do anything really the examples given. The tutorial country to see the result.
useful with SELECT. database provided with InterBase The data values must appear in
In the next issue we’ll cover a lot defines some data validation and the same order as the columns are
more on SELECT, but now that we’ve referential integrity constraints defined; the first value given will be
got a handle on looking at the data, that may give you errors if you inserted into the first column, the
we’ll turn to altering the data. don’t modify the data just right second value into the second