WRITING POSTGRESQL QUERIES
RELATIONS:
CREATE TABLE Movies(
movieID INT,
year INT UNIQUE,
rating CHAR(1),
length INT,
totalEarned NUMERIC(7,2),
PRIMARY KEY(movieID)
);
CREATE TABLE Theaters(
theaterID INT,
address VARCHAR(40) UNIQUE,
numSeats INT NOT NULL,
PRIMARY KEY(theaterID)
);
CREATE TABLE TheaterSeats(
theaterID INT,
seatNum INT,
brokenSeat BOOLEAN NOT NULL,
PRIMARY KEY(theaterID, seatNum),
FOREIGN KEY(theaterID) REFERENCES Theaters
);
CREATE TABLE Showings(
theaterID INT,
showingDate DATE,
startTime TIME,
movieID INT,
priceCode CHAR(1),
PRIMARY KEY(theaterID, showingDate, startTime),
FOREIGN KEY(theaterID) REFERENCES Theaters,
FOREIGN KEY(movieID) REFERENCES Movies
);
CREATE TABLE Customers(
customerID INT,
name VARCHAR(30) UNIQUE,
address VARCHAR(40) UNIQUE,
joinDate DATE,
status CHAR(1),
PRIMARY KEY(customerID)
);
CREATE TABLE Tickets(
theaterID INT,
seatNum INT,
showingDate DATE,
startTime TIME,
customerID INT,
ticketPrice NUMERIC(4,2),
PRIMARY KEY(theaterID, seatNum, showingDate,
startTime),
FOREIGN KEY(customerID) REFERENCES Customers,
FOREIGN KEY(theaterID, seatNum) REFERENCES
TheaterSeats,
FOREIGN KEY(theaterID, showingDate, startTime)
REFERENCES Showings
);
QUERIES TO WRITE:
Query 1 Find all the theaters that have a broken seat. No duplicates should appear in your answer.
Query 2 Find the name and year of all movies for which a customer named Donald Duck bought a ticket. No duplicates should appear in your answer.
Query 3 Find the ID, name, year and length for every movie which was longer than the 2011 movie Avengers. In your result, movies with the largest year should appear first; within each year, movies should be in alphabetized by name. No duplicates should appear in your answer.
Query 4 Find the ID and name of each customer whose name has the letter ‘a’ or ‘A’ anywhere in it, and who bought tickets to at least 2 different movies. Careful; a customer who bought 2 or more tickets to the same movie doesn't qualify. No duplicates should appear in your answer
Query 5 For each ticket for which all of the following are true:
a) the ticket was bought by a customer whose name starts with ‘D’ (capital D),
b) the ticket is for a showing whose price code isn't NULL,
and c) the ticket is on a date between June 1, 2019 and June 30, 2019 (including those dates),
and d) the ticket is for a theater that has more than 5 seats,
Output the ID, name and address of the customer, the address and number of seats of the theater, and the price code for the showing. The 6 attributes in your result should appear as custID, custName, custAddress, theaterAddress, theaterSeats and priceCode. No duplicates should appear in your result.
Query 1:
Select distinct theaterID from TheaterSeats where
brokenSeat='True';

Query 2:
Select distinct Movies.movieID,Movies.year from Tickets inner join
Customers on Tickets.customerID=Customers.customerID inner join
Showings on Showings.theaterID=Tickets.theaterID inner join Movies
on Showings.movieID=Movies.movieID where Customers.name='Donald
Duck'

Query 3:
Select * from Movies where length>(Select length from movies
where year=2011) order by year;

Query 4:
Select Customers.customerID,Customers.name from Customers where
customerID=(Select customerID from Tickets group by customerID
having count(*)>=2) and customerID=(Select Customers.customerID
from Customers where Customers.name like 'a%' or Customers.name
like 'A%');

Query 5:
Select * From Tickets where customerID in(Select customerID from Customers where name like 'D%') and theaterID in(Select theaterID from Showings where priceCode is not null) and theaterID in(Select theaterID from Theaters where numSeats>5) and showingDate between '01-jun-2019' and '30-jun-2019';

if you still have any Problem regarding this question please comment and if you like my code please appreciate me by thumbs up thank you.........
WRITING POSTGRESQL QUERIES RELATIONS: CREATE TABLE Movies( movieID INT, year INT UNIQUE, rating...
SQL CHECK CONSTRAINT AND TEST CASE... SQL Query Here are the tables: CREATE TABLE Movies( movieID INT, name VARCHAR(30) NOT NULL, year INT, rating CHAR(1), length INT, totalEarned NUMERIC(7,2), PRIMARY KEY(movieID), UNIQUE(name, year) ); CREATE TABLE Showings( theaterID INT, showingDate DATE, startTime TIME, movieID INT, priceCode CHAR(1), PRIMARY KEY(theaterID, showingDate, startTime), FOREIGN KEY(theaterID) REFERENCES Theaters, FOREIGN KEY(movieID) REFERENCES Movies ); CREATE TABLE Tickets( theaterID INT, seatNum INT, showingDate DATE, startTime TIME, customerID INT, ticketPrice NUMERIC(4,2), PRIMARY KEY(theaterID, seatNum, showingDate, startTime)...
CREATE TABLE Gender ( gender CHAR(1), description VARCHAR(10), PRIMARY KEY (gender) ); CREATE TABLE People ( ID INT, name VARCHAR(50), gender CHAR(1), height FLOAT, PRIMARY KEY (ID), FOREIGN KEY (gender) REFERENCES Gender (gender) ); CREATE TABLE Sports ( ID INT, name VARCHAR(50), record FLOAT, PRIMARY KEY (ID), UNIQUE (name) ); CREATE TABLE Competitions ( ID INT, place VARCHAR(50), held DATE, PRIMARY KEY (ID) ); CREATE TABLE Results ( peopleID INT NOT NULL, competitionID INT NOT NULL, sportID INT NOT NULL,...
Use an INSERT INTO statement with a subquery containing a SQL string function to create user names for the volunteers based on their legal names and insert them into the table. +----------------------+ | Tables_in_volunteers | +----------------------+ | address | | email | | funds | | hours | | person | | phone | | users | +----------------------+ users legal names are in the address table user table is : CREATE TABLE USERS(volunteer_id INT NOT NULL, username VARCHAR(50), PRIMARY KEY...
Create a new database and execute the code below in SQL Server’s query window to create the database tables. CREATE TABLE PhysicianSpecialties (SpecialtyID integer, SpecialtyName varchar(50), CONSTRAINT PK_PhysicianSpecialties PRIMARY KEY (SpecialtyID)) go CREATE TABLE ZipCodes (ZipCode varchar(10), City varchar(50), State varchar(2), CONSTRAINT PK_ZipCodes PRIMARY KEY (ZipCode)) go CREATE TABLE PhysicianPractices (PracticeID integer, PracticeName varchar(50), Address_Line1 varchar(50), Address_Line2 varchar(50), ZipCode varchar(10), Phone varchar(14), Fax varchar(14), WebsiteURL varchar(50), CONSTRAINT PK_PhysicianPractices PRIMARY KEY (PracticeID), CONSTRAINT FK_PhysicianPractices_ZipCodes FOREIGN KEY (ZipCode) REFERENCES Zipcodes) go CREATE...
Database Management
6. [5] Create the following table: CREATE TABLE customer ( cust_name VARCHAR(30) NOT NULL, address VARCHAR(60), UNIQUE (cust name, address)); A. Run the following inserts and explain why both work and how will you prevent it INSERT INTO customer VALUES ('Alex Doe', NULL); INSERT INTO customer VALUES ('Alex Doe', NULL); 7. [5] Modify the following table definition to ensure that all employees have a minimum wage of $10 CREATE TABLE employee ( empId INT PRIMARY KEY, empName VARCHAR(40)...
Hello, Does anyone have any input on how to fragment this table in visual studio? CREATE TABLE [dbo].[Stock] ( [itemNo] INT NOT NULL , [store] VARCHAR(50) NOT NULL, [qtyOnHand] INT NULL, [qtyHand] INT NULL, [qtyOnOrder] INT NULL, [reorderPoint] INT NULL, CONSTRAINT [FK_Stock_Item] FOREIGN KEY ([itemNo]) REFERENCES [Item]([itemNo]), CONSTRAINT [FK_Stock_Store] FOREIGN KEY ([store]) REFERENCES [Store]([storeName]), CONSTRAINT [PK_Stock] PRIMARY KEY ([itemNo], [store]), The other tables are: CREATE TABLE [dbo].[Item] ( [itemNo] INT NOT NULL PRIMARY KEY, [itemName] VARCHAR(50) NULL, [supplier] VARCHAR(50) NULL,...
SQL queries and procedures TABLE: Employees Business Rules: EmployeeID is defined as the primary key. Address has been denormalized to include City and State for performance reasons. DeptNbr is a foreign key that references DeptNbr in the Department table Manager is a foreign key that references EmployeeID in the Employees table Data Structure: (EmployeeId, LastName, FirstName, Street, City, State, Zip, DateOfHire, DateOfBirth, JobTitle, Salary, DeptNbr(fk), Manager(fk)) TABLE: Department Business Rules: DeptNbr is defined as the primary key. Data Structure: (DeptNbr,...
-- Schema definition
create table Customer (
cid smallint not null,
name varchar(20),
city varchar(15),
constraint customer_pk
primary key (cid)
);
create table Club (
club varchar(15) not null,
desc varchar(50),
constraint club_pk
primary key
(club)
);
create table Member (
club varchar(15) not null,
cid
smallint not null,
constraint member_pk
primary key (club,
cid),
constraint mem_fk_club
foreign key (club)
references Club,
constraint mem_fk_cust...
Given the schema, write a query and subquery
to do the following:
CREATE TABLE Majors major VARCHAR(12), description VARCHAR, PRIMARY KEY (major) ); CREATE TABLE Course ( courseMajor VARCHAR(12), courseNo VARCHAR(6), credits INTEGER NOT NULL, enroll_limit INTEGER, PRIMARY KEY(courseNo, courseMajor), FOREIGN KEY (courseMajor) REFERENCES Majors (major) CREATE TABLE Tracks trackMajor VARCHAR(12), trackCode VARCHAR(10), title VARCHAR, PRIMARY KEY(trackMajor, trackCode), FOREIGN KEY (trackMajor) REFERENCES Majors(major) CREATE TABLE Student ( SID CHAR(8), sName VARCHAR(30), studentMajor VARCHAR(12), trackCode VARCHAR(10), PRIMARY KEY(SID), FOREIGN KEY (studentMajor,...
create table candidate ( cand_id varchar(12) primary key, -- cand_id name varchar(40) -- cand_nm ); create table contributor ( contbr_id integer primary key, name varchar(40), -- contbr_nm city varchar(40), -- contbr_city state varchar(40), -- contbr_st zip varchar(20), -- contbr_zip employer varchar(60), -- contbr_employer occupation varchar(40) -- contbr_occupation ); create table contribution ( contb_id integer primary key, cand_id varchar(12), --...