19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

question mark st<strong>and</strong>s for a single character in the same position as the question mark. For<br />

instance, the pattern “B?d” is matched by “Bid”, “Bud”, <strong>and</strong> “Bad”. An asterisk st<strong>and</strong>s for<br />

any number of characters in the same position as the asterisk. For instance, the pattern “C*r”<br />

is matched by “<strong>Computer</strong>”, “Chair”, <strong>and</strong> “Car”. See Comments 3 through 5 for further information<br />

about Like.<br />

In the sentence<br />

SELECT fields FROM clause<br />

fields is either * (to indicate all fields) or a sequence of the fields to be available (separated<br />

by commas), <strong>and</strong> clause is either a single table or a join of two tables. A join of two tables is<br />

indicated by a clause of the form<br />

table1 INNER JOIN table2 ON foreign key of table1=primary key of table2<br />

Appending<br />

WHERE criteria<br />

to the end of the sentence restricts the records to those satisfying criteria. Appending<br />

ORDER BY field(s) ASC (or DESC)<br />

presents the records ordered by the specified field or fields.<br />

In general, the SQL statements we consider will look like<br />

SELECT www FROM xxx WHERE yyy ORDER BY zzz<br />

where SELECT www FROM xxx is always present <strong>and</strong> accompanied by one or both of WHERE<br />

yyy <strong>and</strong> ORDER BY zzz. In addition, the xxx portion might contain an INNER JOIN phrase.<br />

The settings for the examples mentioned earlier are as follows:<br />

I (a) Show the records from Cities in alphabetical order based on the name of the city.<br />

SELECT * FROM Cities ORDER BY city ASC<br />

I (b) Show the records from Cities in alphabetical order based first on the name of the<br />

country <strong>and</strong>,, within each country group,, the name of the city.<br />

SELECT * FROM Cities ORDER BY country,, city ASC<br />

I (c) Show the records from Cities in descending order based on the projected 2015<br />

population.<br />

SELECT * FROM Cities ORDER BY pop2015 DESC<br />

II (a) Show the records for the Cities in China.<br />

SELECT * FROM Cities WHERE country = ‘China’<br />

II (b) Show the records from Cities whose 2015 population is projected to be at least 20<br />

million.<br />

SELECT * FROM Cities WHERE pop2015 >= 20<br />

II (c) Show the records from Cities whose name begins with the letter S.<br />

SELECT * FROM Cities WHERE city Like ‘S*’<br />

III (a) Show the records from the joined table in descending order of the populations of<br />

their countries.<br />

SELECT * FROM Cities INNER JOIN Countries ON Cities.country =<br />

Countries.country ORDER BY Countries.pop1995 DESC<br />

Relational Databases <strong>and</strong> SQL 333

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!