Ch 8 Pretest Flashcards

1
Q

The Member table will have the following columns:

  • ID—positive integer
  • FirstName—variable-length string with up to 100 characters
  • MiddleInitial—fixed-length string with 1 character
  • LastName—variable-length string with up to 100 characters
  • DateOfBirth—date
  • AnnualPledge—positive decimal value representing a cost of up to $999,999, with 2 digits for cents

Write a SQL statement to create the Member table.

A

CREATE TABLE Member (
ID INT UNSIGNED,
FirstName VARCHAR(100),
MiddleInitial CHAR(1),
LastName VARCHAR(100),
DateOfBirth DATE,
AnnualPledge DECIMAL(8,2) UNSIGNED
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The Rating table has the following columns:

  • RatingCode—variable-length string, primary key
  • RatingDescription—variable-length string

The Movie table should have the following columns:

  • Title—variable-length string, maximum 30 characters
  • RatingCode—variable-length string, maximum 5 characters

Write a SQL statement to create the Movie table. Designate the RatingCode column in the Movie table as a foreign key to the RatingCode column in the Rating table.

A

CREATE TABLE Movie (
Title VARCHAR(30),
RatingCode VARCHAR(5),
FOREIGN KEY(RatingCode) REFERENCES Rating(RatingCode)
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The Movie table has the following columns:

  • ID—integer, primary key
  • Title—variable-length string
  • Genre—variable-length string
  • RatingCode—variable-length string
  • Year—integer

A new column must be added to the Movie table:

  • Column name: Score
  • Data type: decimal(3,1)

Write a SQL statement to add the Score column to the Movie table.

A

ALTER TABLE Movie
ADD Score Decimal(3,1);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The Movie table has the following columns:

  • ID—integer, primary key
  • Title—variable-length string
  • Genre—variable-length string
  • RatingCode—variable-length string
  • Year—integer

Write a SQL statement to create a view named MyMovies that contains the Title, Genre, and Year columns for all movies. Ensure your result set returns the columns in the order indicated.

A

CREATE VIEW MyMovies AS
SELECT Title, Genre, Year
FROM Movie;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

A database has a view named MovieView.

Write a SQL statement to delete the view named MovieView from the database.

A

DROP VIEW MovieView;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The Movie table has the following columns:

  • ID—integer
  • Title—variable-length string
  • Genre—variable-length string
  • RatingCode—variable-length string
  • Year—integer

Write a SQL statement to modify the Movie table to make the ID column the primary key.

A

ALTER TABLE Movie
ADD PRIMARY KEY (ID);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The Movie table has the following columns:

  • ID—integer, primary key
  • Title—variable-length string
  • Genre—variable-length string
  • RatingCode—variable-length string
  • Year—integer

The YearStats table has the following columns:

  • Year—integer
  • TotalGross—bigint unsigned
  • Releases—integer

Write a SQL statement to designate the Year column in the Movie table as a foreign key to the Year column in the YearStats table.

A

ALTER TABLE Movie
ADD FOREIGN KEY (Year) REFERENCES YearStats(Year);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The Movie table has the following columns:

  • ID—integer, primary key
  • Title—variable-length string
  • Genre—variable-length string
  • RatingCode—variable-length string
  • Year—integer

Write a SQL statement to create an index named idx_year on the Year column of the Movie table.

A

CREATE INDEX idx_year
ON Movie (Year);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The Movie table has the following columns:

  • ID—integer, primary key, auto-increment
  • Title—variable-length string
  • Genre—variable-length string
  • RatingCode—variable-length string
  • Year—integer

The following data needs to be added to the Movie table:

|Title|Genre|RatingCode|Year|
|—|—|—|—|
|Pride and Prejudice|Romance|G|2005|

Write a SQL statement to insert the indicated data into the Movie table.

A

INSERT Movie (Title, Genre, RatingCode, Year)
VALUES (‘Pride and Prejudice’, ‘Romance’, ‘G’, 2005);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The Movie table has the following columns:

  • ID—integer, primary key
  • Title—variable-length string
  • Genre—variable-length string
  • RatingCode—variable-length string
  • Year—integer

Write a SQL statement to delete the row with the ID value of 3 from the Movie table.

A

To remove COLUMNS, you would use an ALTER TABLE statement. For this question it is asking to delete a row. We use a DELETE & WHERE clause.

DELETE FROM Movie
WHERE ID = 3;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The Movie table has the following columns:

  • ID—integer, primary key
  • Title—variable-length string
  • Genre—variable-length string
  • RatingCode—variable-length string
  • Year—integer

Write a SQL statement to update the Year value to be 2022 for all movies with a Year value of 2020.

A

UPDATE Movie
SET Year = 2022
WHERE Year = 2020;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

The database contains a table named Movie.

Write a SQL query to return all data from the Movie table without directly referencing any column names.

A

SELECT *
FROM Movie;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

The Movie table has the following columns:

  • ID—integer, primary key
  • Title—variable-length string
  • Genre—variable-length string
  • RatingCode—variable-length string
  • Year—integer

Write a SQL query to retrieve the Title and Genre values for all records in the Movie table with a Year value of 2020. Ensure your result set returns the columns in the order indicated.

A

Select Title, Genre
FROM Movie
WHERE Year = 2020;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

The Movie table has the following columns:

  • ID—integer, primary key
  • Title—variable-length string
  • Genre—variable-length string
  • RatingCode—variable-length string
  • Year—integer

Write a SQL query to display all Title values in alphabetical order A–Z.

A

SELECT Title
FROM Movie
ORDER BY Title;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

The Movie table has the following columns:

  • ID—integer, primary key
  • Title—variable-length string
  • Genre—variable-length string
  • RatingCode—variable-length string
  • Year—integer

Write a SQL query to output the unique RatingCode values and the number of movies with each rating value from the Movie table as RatingCodeCount. Sort the results by the RatingCode in alphabetical order A–Z. Ensure your result set returns the columns in the order indicated.

A

SELECT RatingCode, COUNT(*) AS RatingCodeCount
FROM Movie
GROUP BY RatingCode
ORDER BY RatingCode ASC;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

The Movie table has the following columns:

  • ID—integer, primary key
  • Title—variable-length string
  • Genre—variable-length string
  • RatingCode—variable-length string
  • Year—integer

The YearStats table has the following columns:

  • Year—integer
  • TotalGross—bigint unsigned
  • Releases—integer

Write a SQL query to display both the Title and the TotalGross (if available) for all movies. Ensure your result set returns the columns in the order indicated.

A

SELECT Movie.Title, YearStats.TotalGross
FROM Movie
LEFT JOIN YearStats ON Movie.Year = YearStats.Year;

17
Q

The Movie table has the following columns:

  • ID—integer, primary key
  • Title—variable-length string
  • Genre—variable-length string
  • RatingCode—variable-length string
  • Year—integer

Write a SQL query to return how many movies have a Year value of 2019.

A

SELECT COUNT(*)
FROM Movie
WHERE Year = 2019;

18
Q

There are different ways of referencing columns in different tables. What are the different scenarios and how do you reference in each of those scenarios?

A

Scenarios:

  • Creating a foreign key inside of a table creation statement. We’ll say the col you are using is table A and the one you are referencing is table B.
    • After all of your columns are created, you say:

FOREIGN KEY(TblA_Col_Name) REFERENCES TblB_Name(TblB_Col)

  • Doing joins

SELECT Department.Name AS Group,
Employee.Name AS Supervisor
FROM Department
Employee
ON Manager = ID;

19
Q

How do you get rid of ROWS containing specific values?

A

DELETE FROM TableName
WHERE ColName = X;

20
Q

How do you update ROWS containing specific values?

A

UPDATE TableName
SET ColName = NewValue
WHERE ColName = Condition;

21
Q

What does the GROUP BY statement do?

A

The GROUP BY statement groups rows that have the same values into summary rows, like “find the number of customers in each country”.

The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.