The document contains examples of SQL queries:
1) Queries to select data from the pet table based on various criteria like owner, birthdate, sex, species.
2) Queries to display specific columns, order results, filter on name patterns.
3) Aggregate functions like count are used to count total animals, pets per owner, and animals per species.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
84 views
Database Lab 2 Answers - Pet
The document contains examples of SQL queries:
1) Queries to select data from the pet table based on various criteria like owner, birthdate, sex, species.
2) Queries to display specific columns, order results, filter on name patterns.
3) Aggregate functions like count are used to count total animals, pets per owner, and animals per species.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
a) Selecting All Data
select * from cisb314.pet;
b) Display the records for owner Gwen.
select * from cisb314.pet where Owner="Gwen";
c) Display the animals that were born during or after 2014
select * from cisb314.pet where Birth like '2014-01-01';
d) Display records for female dogs.
select * from cisb314.pet where Species="dog" AND Sex="f";
e) Display records for snake and bird.
select * from cisb314.pet where Species="snake" or Species="bird";
f) Display records for male cat and female dog.
select * from cisb314.pet where (Sex="m" and Species="cat") or (Sex="f" and Species="dog");
g) Display columns name and birth.
select PetName, Birth from pet;
h) Display the pets’ owners.
select Owner from pet;
i) Display pets’ owner only once.
select distinct Owner from pet; j) Display name, species and birth for dogs and cats only. select PetName, Species, Birth from pet where Species='cat' or Species='dog';
k) Display columns name and birth sorted by birthdate in ascending order.
select PetName, Birth from pet order by Birth ASC;
l) Display records for name begins with letter ‘B’.
select * from cisb314.pet where PetName LIKE "b%";
m) Display records for name ends with letter ‘y’.
select * from cisb314.pet where PetName LIKE "%y";
n) Display records for name contains letter ‘a’.
select * from cisb314.pet where PetName LIKE "%a%";
o) Display records for name contains exactly 5 characters.
i) select * from cisb314.pet where LENGTH (Trim(PetName))=5; ii) select * from cisb314.pet where Petname LIKE '_____';
p. Count the number of animals.
select count(*) from pet;
q. Count how many pets each owner has.
select Owner, count(*) from pet group by Owner;
r. Count the number of animals per species.
select Species, count(*) from pet group by Species;