SELECT shrfirm, shrcode FROM shr;
SELECT * FROM shr WHERE shrprice < 1.00;
shrcode | shrfirm | shrprice | shrqty | shrdiv | shrpe |
---|---|---|---|---|---|
BE | Burmese Elephant | $0.07 | 154,713 | $0.01 | 3 |
SELECT shrfirm, shrprice FROM shr WHERE shrprice >= 10.00;
SELECT shrfirm, shrprice, shrqty, shrqty*shrprice FROM shr;
shrfirm |
shrprice | shrqty | expr1003 |
---|---|---|---|
Abyssinian Ruby | $31.82 | 22,010 | $700,358.20 |
Burmese Elephant | $0.07 | 154,713 | $10,829.91 |
Bolivian Sheep | $12.75 | 231,678 | $2,953,894.50 |
Canadian Sugar | $52.78 | 4,716 | $248,910.48 |
Freedonia Copper | $27.50 | 10,529 | $289,547.50 |
Indian Lead & Zinc | $37.75 | 6,390 | $241,222.50 |
Nigerian Geese | $35.00 | 12,323 | $431,305.00 |
Patagonian Tea | $55.25 | 12,635 | $698,083.75 |
Royal Ostrich Farms | $33.75 | 1,234,923 | $41,678,651.25 |
Sri Lankan Gold | $50.37 | 32,868 | $1,655,561.16 |
SELECT shrfirm, shrdiv/shrprice*100 FROM shr WHERE shrdiv/shrprice*100 > 5;
SELECT shrdiv*shrqty FROM shr WHERE shrfirm = 'Patagonian Tea';
expr1000 |
---|
$31,587.50 |
SELECT shrcode, shrfirm FROM shr WHERE shrprice < 20*shrdiv;
SELECT shrcode, shrfirm, shrdiv/shrprice*100 FROM shr WHERE shrdiv/shrprice = (SELECT MIN(shrdiv/shrprice) FROM shr);
shrfirm |
expr1001 |
---|---|
Abyssinian Ruby | 4.14833438089252 |
SELECT SUM (shrprice*shrqty) FROM shr WHERE shrpe > 10;
SELECT shrcode, shrfirm FROM shr WHERE shrdiv*shrqty = (SELECT MAX(shrdiv*shrqty) FROM shr);
shrfirm |
---|
Royal Ostrich Farms |
SELECT shrfirm, shrprice*shrqty FROM shr WHERE shrfirm IN ('Abyssinian Ruby', 'Sri Lankan Gold');
SELECT shrcode, shrfirm, shrdiv/shrprice*100 FROM shr WHERE shrfirm NOT IN ('Bolivian Sheep', 'Canadian Sugar');
shrfirm |
expr1001 |
---|---|
Freedonia Copper | 6.69090909090909 |
Patagonian Tea | 4.52488687782805 |
Abyssinian Ruby | 4.14833438089252 |
Sri Lankan Gold | 5.3206273575541 |
Indian Lead & Zinc | 7.94701986754967 |
Burmese Elephant | 14.2857142857143 |
Nigerian Geese | 4.8 |
Royal Ostrich Farms | 8.88888888888889 |
SELECT SUM (shrprice*shrqty) FROM shr;
SELECT shrfirm, shrprice*shrqty FROM shr ORDER BY shrprice*shrqty DESC;
shrfirm |
expr1001 |
---|---|
Royal Ostrich Farms | $41,678,651.25 |
Bolivian Sheep | $2,953,894.50 |
Sri Lankan Gold | $1,655,561.16 |
Abyssinian Ruby | $700,358.20 |
Patagonian Tea | $698,083.75 |
Nigerian Geese | $431,305.00 |
Freedonia Copper | $289,547.50 |
Canadian Sugar | $248,910.48 |
Indian Lead & Zinc | $241,222.50 |
Burmese Elephant | $10,829.91 |
SELECT shrcode,shrfirm FROM shr WHERE shrfirm REGEXP 'Gold';
SELECT shrcode, shrfirm FROM shr WHERE shrcode REGEXP '^B';
shrfirm |
---|
Burmese Elephant |
Bolivian Sheep |
Question | Answer | |
a. | SELECT shrfirm FROM shr WHERE shrfirm NOT REGEXP 's' | List shares with a firm name containing no lower case s. |
b. | SELECT shrfirm FROM shr WHERE shrfirm NOT REGEXP 'S' | List shares with a firm name containing no upper case s. |
c. | SELECT shrfirm FROM shr WHERE shrfirm NOT REGEXP 's' AND shrfirm NOT REGEXP 'S' | List all shares except those with a firm name containing an upper and a lower case s. |
d. | SELECT shrfirm FROM shr WHERE shrfirm NOT REGEXP 's' OR shrfirm NOT REGEXP 'S' | List all shares with a firm name containing no upper or lower case s. |
e. | SELECT shrfirm FROM shr WHERE (shrfirm NOT REGEXP 's' AND shrfirm NOT REGEXP 'S') OR shrfirm REGEXP '^S' | List all shares for firms whose name does not contain an upper and lower case s and list those firms whose names begin with an S. |
CREATE TABLE alien ( alno INTEGER, alname VARCHAR(20), alheads INTEGER, alcolor VARCHAR(15), alsmell VARCHAR(20), PRIMARY KEY (alno));
SELECT AVG(alheads) FROM alien;
expr1000 |
---|
2.4 |
SELECT alname, alheads FROM alien WHERE alheads = (SELECT MAX(alheads) FROM alien);
SELECT alname FROM alien WHERE alname REGEXP '[o]{2}';
alname |
---|
Foozelgiga |
Koofoopsiepus |
SELECT COUNT(*) FROM alien WHERE alcolor = 'chartreuse';
SELECT * FROM alien ORDER BY alsmell, alcolor;
alnum |
alname |
alheads |
alcolor |
alsmell |
---|---|---|---|---|
1 | Foozelgiga | 1 | vermillion | ambrosial |
5 | Eop | 0 | chartreuse | new car |
4 | Meekamuncha | 7 | lavender | old cabbage |
3 | Bunyippa | 1 | chartreuse | perfumed |
2 | Koofoopsiepus | 3 | amethyst | putrid |
CREATE TABLE book ( isbn CHAR(10), author VARCHAR(30), title VARCHAR(30), edition CHAR(2), type VARCHAR(20), publisher VARCHAR(20), year INTEGER, PRIMARY KEY (isbn)
Sample SQL queries:
SELECT isbn, author, title FROM book WHERE author = 'Wesner, Rudy'; SELECT * FROM book WHERE title REGEXP 'Mocking Bird';
The ISBN number is a unique identifier for each book. However, it did not appear until around the 1960s so would not be useful for a collection containing some old books.
Eduardo could physically organize his books on shelves. There are two alternatives to arranging these books. Eduardo could simply arrange them by ISBN number. This solution would help him quickly find a book when he is using the database. However, if he knows something about the book he is searching for, it may be more practical to order the books by author. This way he would be able to quickly find the book by Watson, Richard.
One shortcoming of this database is that it does not consider the possibility that Eduardo may actually have two copies of the same book. Also, there is no place to enter any detail information on the author or the publisher of the book.
Also, there is no possibility to enter information on the short story of a particular author published in a book of short stories. All of these shortcomings can be redeemed with a database containing several entities.
An identifier is an attribute of an entity which uniquely describes an instance of this entity. In other words, the value for this identifier must be different for each instance of the entity.
A data model has an identifier so that each instance of the entity can be identified uniquely. For example, customer last name would not be a good identifier since there could be two customers with the same name, e.g., Miller. If one of these customers named Miller complains about his transaction, it is impossible for the system to know which Miller has filed the complaint unless some additional information is available.
Entities are things existing in the real world. Examples of entities are people, cars, and houses.
The entity integrity rule states that each instance of an entity must have a unique identifier, which is not null.
This page is part of the promotional and support
material for Data Management (open edition) by Richard T. Watson For questions and comments please contact the author |