Question

-- Table construction (just 1 simplest possible way) CREATE TABLE PetType ( petTypeId VARCHAR(10) PRIMARY KEY,...

-- Table construction (just 1 simplest possible way)
CREATE TABLE PetType (
petTypeId VARCHAR(10) PRIMARY KEY,
animalType VARCHAR(20),
breed VARCHAR(20)
);
CREATE TABLE Owner (
ownerId VARCHAR(10) PRIMARY KEY,
firstName VARCHAR(20),
lastName VARCHAR(20) NOT NULL,
homePhoneNumber VARCHAR(20),
streetAddress VARCHAR(80),
suburb VARCHAR(20),
postcode VARCHAR(10)
);
CREATE TABLE Pet (
petId VARCHAR(10) PRIMARY KEY,
petName VARCHAR(20),
sex CHAR(1)
CHECK (sex IN ('M', 'F')),
petTypeId VARCHAR(10)
FOREIGN KEY REFERENCES PetType
);
CREATE TABLE PetAndOwner (
ownerId VARCHAR(10),
petId VARCHAR(10),
PRIMARY KEY (ownerId, petId),
FOREIGN KEY (ownerId) REFERENCES Owner,
FOREIGN KEY (petId) REFERENCES Pet
);
-- Populate the tables with simple data
INSERT INTO PetType VALUES ('001', 'dog', 'Bulldog');
INSERT INTO PetType VALUES ('002', 'dog', 'Lhasa Apso');
INSERT INTO PetType VALUES ('003', 'dog', 'Maltese');
INSERT INTO PetType VALUES ('004', 'cat', 'Persian');
INSERT INTO PetType VALUES ('005', 'cat', 'Ragdoll');
INSERT INTO Owner VALUES ('001', 'David', 'Smith', '12345678',
'100 Victoria Road', 'Rydalmere', '2116');
INSERT INTO Owner VALUES ('002', 'Louise', 'Brown', '87654321',
'1 James Ruse Road', 'Rydalmere', '2116');
INSERT INTO Owner VALUES ('003', 'Robert', 'Brown', '11223344',
'2 Wentworth Street', 'Parramatta', '2150');
INSERT INTO Owner VALUES ('004', 'Avatar', 'Phantom', '',
'1 Pandora', 'Na''vi Land', '0000');
INSERT INTO Pet VALUES ('001', 'Mickey Mouse', 'M', '001');
INSERT INTO Pet VALUES ('002', 'Bugs Bunny', 'M', '001');
INSERT INTO Pet VALUES ('003', 'Betty Boop', 'F', '002');
INSERT INTO Pet VALUES ('004', 'Droopy', 'M', '003');
INSERT INTO Pet VALUES ('005', 'Penelope', 'F', '004');
INSERT INTO Pet VALUES ('006', 'Jerry', 'F', '005');
INSERT INTO PetAndOwner VALUES ('001', '001');
INSERT INTO PetAndOwner VALUES ('001', '004');
INSERT INTO PetAndOwner VALUES ('002', '001');
INSERT INTO PetAndOwner VALUES ('002', '005');
INSERT INTO PetAndOwner VALUES ('003', '002');

i.) For the Pet Datbase described in the previous practical, write an SQL statement with a subquery to list all the people in the Owner table who own more than one pet. 9. (Aggregate function) Continuing from the previous question, write an SQL statement to list the total number of pets for each pair of pet type and breed. Sort the listing first in the decreasing order of the total number, then in the order of animalType and then in the order of breed.

10. (Multi-table join) Continuing from the previous question, write an SQL statement to list all the pet details for all the owned pets.

Homework Answers

Answer #1

Hi,

Please see the below Querries:

 
 --Querrry to fetch Owners Infor who is more than one Pet
 SELECT OwnerID,
                firstName,
                LastName,
                homephonenumber,
                streetaddress,
                suburb,
                postcode 
FROM ( 
                SELECT PO.OwnerID,
                                O.firstName,
                                O.LastName,
                                O.homephonenumber,
                                O.streetaddress,
                                O.suburb,
                                O.postcode,
                                ROW_NUMBER() OVER(Partition By PO.OwnerID order by PO.OwnerID) 'PetCount'
                   FROM PetAndOwner PO
                                INNER JOIN owner O
                                ON PO.OwnerID = O.OwnerID
        ) AS PetOwnerInfo
   WHERE PetCount > 1

OUTPUT:

 --Continuing from the previous question, write an SQL statement to list the total number of pets for each pair of pet type and breed. 
 --Sort the listing first in the decreasing order of the total number, then in the order of animalType and then in the order of breed 

 SELECT PT.petTypeID,
                PT.animalType,
                PT.breed, 
                count(P.petTypeID) 'TotalNumnerOfPet' 
   FROM PetType PT
                INNER JOIN Pet P 
                ON P.petTypeID = PT.petTypeID
        GROUP BY PT.petTypeID,
                         PT.animalType,
                                 PT.breed
        ORDER BY TotalNumnerOfPet DESC,
                         animaltype,
                                 breed

OUTPUT:

--(Multi-table join) Continuing from the previous question, write an SQL statement to list all the pet details for all the owned pets

SELECT P.PETID,
       P.PETNAME,
           P.SEX,
           O.OWNERID,
           O.FIRSTNAME,
           O.LASTNAME,
           O.HOMEPHONENUMBER,
           O.STREETADDRESS,
           O.SUBURB,
           O.POSTCODE
  FROM Pet P INNER JOIN PetAndOwner PO
           ON P.PETID = PO.PETID
           INNER JOIN Owner O
           ON PO.OWNERID = O.OWNERID

OUTPUT:

THANKS.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1. For the Pet Database described in the previous practical, write an SQL statement with a...
1. For the Pet Database described in the previous practical, write an SQL statement with a subquery to list all the people in the Owner table who own more than one pet. should be a collum of the total number of pets owned, owner id, first name and last name -- DROP TABLE PetAndOwner, Pet, PetType, Owner; CREATE TABLE PetType ( petTypeId VARCHAR(10) PRIMARY KEY, animalType VARCHAR(20), breed VARCHAR(20) ); CREATE TABLE Owner ( ownerId VARCHAR(10) PRIMARY KEY, firstName VARCHAR(20),...
Task 7.1.2. Create a view named nc_jobs which shows all the bookjob information for a job...
Task 7.1.2. Create a view named nc_jobs which shows all the bookjob information for a job with a jobtype of N for publisher with a creditcode of C. CREATE TABLE publishers ( cust_id       CHAR(3) NOT NULL, name        CHAR(10), city        CHAR(10), phone        CHAR(8), creditcode    CHAR(1), primary key (cust_id) ); CREATE TABLE bookjobs ( job_id       CHAR(3) NOT NULL, cust_id       CHAR(3), job_date    DATE, descr CHAR(10), jobtype CHAR(1), primary key (job_id), foreign key...
Space X Bank CREATE TABLE Branch(BranchIDNumber VARCHAR(15) PRIMARY KEY NOT NULL, BranchName VARCHAR(6) NOT NULL, Address...
Space X Bank CREATE TABLE Branch(BranchIDNumber VARCHAR(15) PRIMARY KEY NOT NULL, BranchName VARCHAR(6) NOT NULL, Address VARCHAR(50) NOT NULL, City TEXT NULL, State CHAR(2) NULL, ZipCode INT(11) NOT NULL, OfficeNum VARCHAR(15) NOT NULL, FaxNum VARCHAR(15) NOT NULL); CREATE TABLE Employee(EmployeeIDNumber VARCHAR(15) NOT NULL,FirstName VARCHAR(35) NOT NULL, LastName VARCHAR(35) NOT NULL, Email VARCHAR(100) NOT NULL, BranchIDNumber VARCHAR(11) NOT NULL, FOREIGN KEY(BranchIDNumber) REFERENCES Branch(BranchIDNumber), JobTitle ENUM("Manager","Staff") NOT NULL, Salary DECIMAL(8, 2) NOT NULL, HomeNumber VARCHAR(13) NULL, CellNumber VARCHAR(13) NOT NULL); CREATE TABLE...
SQL Data: you can just copy paste it into mysql to. CREATE DATABASE University;USE University; CREATE...
SQL Data: you can just copy paste it into mysql to. CREATE DATABASE University;USE University; CREATE TABLE Student (  sid INT PRIMARY KEY,  name VARCHAR(20) NOT NULL,  address VARCHAR(20) NOT NULL,  major CHAR(2)); CREATE TABLE Professor (  pid INT PRIMARY KEY,  name VARCHAR(20) NOT NULL,  department VARCHAR(10) NOT NULL); CREATE TABLE Course (  cid INT PRIMARY KEY,  title VARCHAR(20) NOT NULL UNIQUE,  credits INT NOT NULL,  area VARCHAR(5) NOT NULL); CREATE TABLE Transcript (  sid INT,  cid INT,  pid INT,  semester VARCHAR(9),  year YEAR,  grade CHAR(1) NOT NULL,  PRIMARY KEY (sid, cid, semester, year),  FOREIGN KEY (sid) REFERENCES Student...
Question 1: Group by and Aggregates: Write SQL statements to answer the following questions using Assignment...
Question 1: Group by and Aggregates: Write SQL statements to answer the following questions using Assignment 4’s schema (Customer-Invoice-Line-Product-Vendor). Make sure that your SQL script runs without any errors. Submit your answers in a .SQL file. 1 (2 Points) - Find the count of distinctvendors thatsupplied products that are priced lowerthan 185? 2 (2 Points) - For each vendor, find their product that has the lowest product quantity. Your output should include vendor code, vendor name, product description and product...
Write the SQL statements for creating a table Event with primary key e_id, and an event...
Write the SQL statements for creating a table Event with primary key e_id, and an event date, e_date that cannot be null. Also write the statements to create a table Participant with primary key p_id, a name that can be up to 30 characters long but may be shorter, and an event e_id as a foreign key that references the corresponding event.
If you had to create a new table in your database called EMPLOYEE and load data...
If you had to create a new table in your database called EMPLOYEE and load data into it using SQL commands below. CREATE TABLE EMPLOYEE (               EMP_ID INT PRIMARY KEY,               EMP_FNAME VARCHAR(200) NOT NULL,               EMP_LNAME VARCHAR(200) NOT NULL,               EMP_CITY VARCHAR(500) NULL ,               EMP_STATE VARCHAR(20) NULL ) INSERT INTO EMPLOYEE VALUE (1, ‘Kevin’, ‘Lailnay’, NULL, ‘PA’) INSERT INTO EMPLOYEE VALUE (2, ‘Sarita, ‘Kawn, ‘Princeton’,’NJ’) INSERT INTO EMPLOYEE VALUE (2, ‘Gloria, ‘Pitts, ‘Allentown, ‘PA’) INSERT INTO EMPLOYEE...
SQL DATABASE Task 4 [1.5 marks] Create Index (0.5 marks) Currently, the database only contains a...
SQL DATABASE Task 4 [1.5 marks] Create Index (0.5 marks) Currently, the database only contains a small number of records. However, the data contained within it is expected to grow significantly in the future. Creating indexes on commonly searched columns is a way performance issues can be minimized. Write a command to create an index on student_name column of the student table. Create view – 1 mark Write a command to create a view to list the student ID and...
Created the database and all the tables in phpMyadmin. In addition, write all the sql statements...
Created the database and all the tables in phpMyadmin. In addition, write all the sql statements used in perforing the tasks in a word document and submit that for grading. Lab 1: CREATE STATEMENT This statement is used to create a database or a table. CREATE DATABASE This statement is used to create a database Syntax: CREATE DATABASE database_name; OR CREATE SCHEMA database_name; Example: CREATE DATABASE Nyumbani; OR CREATE SCHEMA Nyumbani ; CREATE TABLE This SQL statement is used to...
Find the total number of employees and the total number of dependents for every department (the...
Find the total number of employees and the total number of dependents for every department (the number of dependents for a department is the sum of the number of dependents for each employee working for that department). Return the result as department name, total number of employees, and total number of dependents Based on the following table: -- drop tables DROP TABLE EMPLOYEE CASCADE CONSTRAINTS; DROP TABLE DEPARTMENT CASCADE CONSTRAINTS; DROP TABLE DEPT_LOCATIONS CASCADE CONSTRAINTS; DROP TABLE PROJECT CASCADE CONSTRAINTS;...