Final Exam CIT225 Flashcards

1
Q

The following SELECT statement is using what type of table:

mysql> SELECT CONCAT(cust.last_name, ‘, ‘,cust.first_name) AS
full_name
-> FROM (SELECT first_name
-> , last_name
-> , email
-> FROM customer
-> WHERE first_name LIKE ‘JE%’) cust;

It displays the following result set:

+——————+
| full_name |
+——————+
| DAVIS, JENNIFER |
| HALL, JESSICA |
| BELL, JEAN |
| GREENE, JEANETTE |
| LAWSON, JEANNE |
| BANKS, JESSIE |
| TERRY, JENNIE |
| CASTRO, JENNY |
| SPEAR, JEFFREY |
| JORDON, JERRY |
| HURTADO, JEREMY |
| SCHILLING, JESSE |
| PINSON, JEFFERY |
| EAST, JEFF |
| MCCARTNEY, JESUS |
| KENYON, JEROME |
| MILAM, JESSIE |
+——————+
17 rows in set (0.50 sec)

A

Yes, the subquery table in the FROM clause of the outer query can also be referred to as a derived table or an inline view.

A derived table is a virtual table that is created as a result of a subquery, and it can be used in the same way as a physical table. In the given example, the subquery is retrieving data from the “customer” table and filtering it to include only records where the first name starts with ‘JE’. The result of the subquery is then used as a derived table that can be selected from in the outer query.

An inline view is another term used to describe a derived table, and refers to a subquery that is used in the FROM clause of another query. In the given example, the subquery is being used as an inline view in the FROM clause of the outer query.

So, the subquery table in the example can be called a derived table, an inline view, or a subquery table, all of which refer to the same concept of using a subquery to create a virtual table that can be used in another query.

A derived table and a virtual table are both table-like structures that are created as a result of a query, but they have some differences in how they are created and used.

A derived table is created as a result of a subquery that is included in the FROM clause of another query. The subquery is executed first, and its result set is used to create a temporary table, also known as a derived table or an inline view, that can be referenced in the outer query. A derived table is essentially a virtual table that is created on-the-fly from a subquery.

A virtual table, on the other hand, can be created in several ways. It can be created from a view, which is a named query that defines a specific set of columns and rows from one or more tables in the database. It can also be created from a common table expression (CTE), which is a named temporary result set that can be referenced in subsequent queries. In addition, some databases support table-valued functions (TVFs), which are user-defined functions that return a result set that can be used as a virtual table.

In summary, a derived table is a type of virtual table that is created as a result of a subquery in the FROM clause of another query. A virtual table, on the other hand, is a more general term that refers to any table-like structure that is created from a query, including views, CTEs, and TVFs.

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

True or False: A “surrogate key” is generated by database management systems as a unique set of numbers.

A

True

A surrogate key is a unique identifier that is added to a database table to serve as its primary key. It is typically generated by the database management system and is not derived from the data in the table. Surrogate keys are often used when there is no natural or obvious primary key in the data itself.

Surrogate keys can be generated using a variety of techniques, such as using an auto-incrementing integer or a UUID (Universally Unique Identifier). The key is then assigned to each row in the table, providing a unique identifier for that row.

The use of surrogate keys can simplify database design and management, as it eliminates the need to use composite keys or other complex key structures. It also ensures that each row in the table has a unique identifier, making it easier to reference and manipulate data.

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

True or False: A “network database system” can work as a multi-parent hierarchy

A

True

Yes, a network database system can work as a multi-parent hierarchy. In a network database model, data is organized as a collection of records, each containing one or more fields, and each record can have multiple parent and child records. This type of database model is often used for complex data structures that cannot be easily represented in a relational database model.

In a multi-parent hierarchy, a record can have multiple parent records, which allows for more complex relationships between data. This is different from a traditional hierarchy where each record has only one parent record. In a network database model, the relationships between records are represented by pointers or links that connect the child record to one or more parent records.

The ability to represent multi-parent hierarchies in a network database system can be useful in applications where data has complex relationships, such as in project management, supply chain management, or genealogy. However, it is important to note that network database systems can be more complex and difficult to manage than relational database systems, and may require specialized skills and expertise to design and maintain.

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

True or False: SELECT statements retrieve data

A

True

Yes, SELECT statements are used to retrieve data from a database. In SQL (Structured Query Language), SELECT statements are used to query a database table or view and return a result set that contains the data that matches the specified criteria.

A typical SELECT statement consists of the SELECT keyword, a list of columns to retrieve data from (or an asterisk to select all columns), the FROM keyword followed by the name of the table or view, and optional clauses such as WHERE, ORDER BY, GROUP BY, and HAVING.

For example, the following SELECT statement retrieves all the columns from a table named “employees” where the employee’s job title is “Manager”:

SELECT *
FROM employees
WHERE job_title = ‘Manager’;

This statement would return a result set that contains all the columns (e.g., employee ID, name, job title, hire date, etc.) for all employees in the “employees” table whose job title is “Manager.”

SELECT statements are essential for retrieving and analyzing data from a database and are used extensively in data analytics, business intelligence, and other data-related applications.

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

Which of the following are SQL integration toolkits?

A

The term “SQL integration toolkits” can refer to various software tools or frameworks that are designed to help integrate SQL-based systems with other applications or technologies. Some examples of SQL integration toolkits include:

ODBC (Open Database Connectivity) - A standard API for accessing and integrating data from various database management systems using SQL.

JDBC (Java Database Connectivity) - A Java-based API that provides access to SQL databases and allows Java programs to interact with them.

ADO.NET (ActiveX Data Objects .NET) - A data access technology in the .NET Framework that provides a consistent interface for accessing data from different databases using SQL.

SQLAlchemy - A Python-based toolkit that provides a set of high-level APIs for working with SQL-based databases and integrating them with other Python applications.

Django ORM (Object-Relational Mapping) - A Python-based ORM that allows developers to interact with SQL databases using Python objects and provides a high-level abstraction for working with data.

Ruby DBI is a database interface library for Ruby programming language that provides a uniform API for various relational database systems. It supports many databases including MySQL, Oracle, PostgreSQL, SQLite, and more. It is not a SQL integration toolkit per se, but it does allow Ruby programs to interact with SQL-based databases.

Python DB is not a specific tool or framework but rather a term that can refer to any of the various Python-based database APIs, such as Python DB-API, SQLAlchemy, Django ORM, and others. These APIs provide different levels of abstraction and functionality for working with SQL-based databases from within Python.

These are just a few examples of SQL integration toolkits. There are many other tools and frameworks available that can help developers integrate SQL-based systems with other technologies and applications.

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

What was SQL originally created for?

A

SQL (Structured Query Language) was originally created in the early 1970s by IBM researcher Edgar F. Codd as a way to manage and manipulate data stored in relational database systems. It was designed to be a declarative language that allowed users to express complex queries in a concise and easily understandable way.

SQL was originally developed as part of the System R project at IBM, which aimed to create a prototype of a relational database management system. The language was later standardized by the American National Standards Institute (ANSI) and the International Organization for Standardization (ISO), leading to the development of various SQL-based relational database management systems such as Oracle, Microsoft SQL Server, MySQL, and PostgreSQL.

Today, SQL is the standard language for managing and querying data in relational database systems, and is widely used in various industries and applications, including finance, healthcare, e-commerce, and more. The language has evolved over time and has become more feature-rich, with new functionality added to support more complex data structures, analytical processing, and other advanced data-related tasks.

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

The following query returns all columns from the language table.

mysql> SELECT name
-> FROM language;
It returns a result like the following:

+———-+
| name |
+———-+
| English |
| Italian |
| Japanese |
| Mandarin |
| French |
| German |
+———-+
6 rows in set (0.04 sec)

A

False

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

Each “database connection” to the MySQL server is assigned an identifier, which is shown to you when you first log in to your account.

A

No, this statement is incorrect. When you first log in to a MySQL account, you are not assigned a database connection identifier. Instead, a new connection is established to the MySQL server for each client that connects to it.

Each connection to the MySQL server is identified by a unique connection ID, which is automatically assigned by the server. You can obtain the connection ID for a particular connection by running the CONNECTION_ID() function in MySQL. This function returns the ID of the current connection, which is a positive integer value.

It’s important to note that connections to the MySQL server are not the same as user accounts. User accounts are used to authenticate and authorize users to access databases and perform various operations, while connections are used to establish a communication channel between the client and the server for executing SQL statements and retrieving data.

In summary, each client that connects to a MySQL server is assigned a unique connection ID, which identifies the connection during its lifetime. User accounts, on the other hand, are used to authenticate and authorize users to perform various operations within the databases they have access to.

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

A FROM clause with two tables requires a link between the tables inside an ON clause.

A

Yes, in most cases, when you use a FROM clause with two or more tables in a SQL query, you need to specify a join condition in an ON clause to link the tables together. The join condition specifies how the rows from the different tables should be matched based on a common column or set of columns.

For example, consider the following query that joins two tables, orders and customers, using an inner join:

SELECT *
FROM orders
JOIN customers
ON orders.customer_id = customers.customer_id;

In this query, the two tables are linked together using the customer_id column, which is present in both tables. The ON clause specifies the join condition, which is orders.customer_id = customers.customer_id. This condition ensures that only the rows from the two tables that have matching customer_id values are included in the result set.

It’s worth noting that there are different types of join operations, such as inner join, left join, right join, and full outer join, each with their own syntax and rules for specifying the join condition. In general, however, when joining two or more tables in a SQL query, you need to specify a join condition in an ON clause to link the tables together.

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

A “range condition” in the WHERE clause may compare whether a literal, column, or expression values is between a set of literal, column, or expression values that exclude a NULL value.

mysql> SELECT first_name
-> , last_name
-> FROM customer
-> WHERE last_name BETWEEN ‘FA’ AND ‘G’;

A

Yes, a range condition in the WHERE clause of a SQL query can compare whether a literal, column, or expression value is between a set of literal, column, or expression values, and exclude a NULL value.

In the example query you provided, the BETWEEN operator is used to compare the values in the last_name column of the customer table to a range of values, specifically from ‘FA’ to ‘G’. The BETWEEN operator includes both the starting and ending values of the range, so in this case, it would include any last names that start with the letters ‘FA’, ‘FB’, …, ‘G’.

It’s worth noting that when using the BETWEEN operator, the comparison is performed in ascending order. So in the example query, all last names that come between ‘FA’ and ‘G’ in alphabetical order would be included in the result set.

Additionally, when using the BETWEEN operator, it’s important to be aware of how NULL values are handled. By default, NULL values are excluded from the range comparison, so any rows with a NULL value in the last_name column would not be included in the result set. If you want to include NULL values in the range comparison, you can use the IS NULL or IS NOT NULL operators to explicitly test for NULL values.

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

A “range condition” in the WHERE clause may compare whether a literal, column, or expression values of a NULL value, like an optional middle_name column value, is between a set of literal, column, or expression values that exclude a NULL value.

mysql> SELECT first_name
-> , last_name
-> FROM customer
-> WHERE middle_name BETWEEN ‘FA’ AND ‘G’;

A

No, a “range condition” in the WHERE clause cannot compare a NULL value to a set of literal, column, or expression values that exclude NULL.

In the example query you provided, the middle_name column is being compared to a range of values using the BETWEEN operator. If any row in the customer table has a NULL value in the middle_name column, it would not be included in the result set because NULL values are excluded by default when using the BETWEEN operator.

To include rows with NULL values in the comparison, you can use the IS NULL or IS NOT NULL operators. For example, you could modify the query to include rows with NULL middle names by using a WHERE clause like this:

SELECT first_name, last_name
FROM customer
WHERE middle_name BETWEEN ‘FA’ AND ‘G’ OR middle_name IS NULL;

This would include any rows where the middle_name column value is between ‘FA’ and ‘G’, or where the middle_name column is NULL.

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

A “condition” in the WHERE clause may compare inequality, like IN, between literal, column, or expression values and a lists of values.

mysql> SELECT ‘True’ AS answer
-> WHERE ‘a’ IN (SELECT ‘a’ UNION SELECT ‘b’ UNION SELECT ‘c’);

A

Yes, a “condition” in the WHERE clause can compare inequality using operators like IN, which tests if a value matches any value in a list of values.

In the example query you provided, the WHERE clause is using the IN operator to check if the literal value ‘a’ is present in the list of values returned by the subquery. If ‘a’ is present in the subquery, the query will return a single row with the value ‘True’ in the answer column.

Here’s how the query works:

The subquery SELECT ‘a’ UNION SELECT ‘b’ UNION SELECT ‘c’ creates a list of three values: ‘a’, ‘b’, and ‘c’.
The WHERE clause checks if the literal value ‘a’ is present in the list of values from the subquery. Since ‘a’ is present, the condition is true.
The SELECT clause returns a single row with the value ‘True’ in the answer column.
So the output of the query would be:

+——–+
| answer |
+——–+
| True |
+——–+
1 row in set (0.00 sec)

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

A “condition” in the WHERE clause that may compare equality between literal, column, or expression values and use which of the following wildcards?

A

A “condition” in the WHERE clause that may compare equality between literal, column, or expression values can use the wildcard character % for pattern matching.

The % wildcard matches any sequence of zero or more characters, and it can be used in conjunction with the LIKE operator to perform pattern matching. For example, the following query retrieves all rows from the customer table where the last_name column starts with the letter ‘S’:

SELECT *
FROM customer
WHERE last_name LIKE ‘S%’;

This query will match any last name that starts with the letter ‘S’, followed by any sequence of zero or more characters. So it will match ‘Smith’, ‘Simpson’, ‘Santana’, and so on.

Yes, the asterisk symbol (*) is a wildcard character that is used in SQL to select all columns or all rows from a table.

When used in a SELECT statement, * selects all columns from the specified table. For example, the following query selects all columns from the customer table:

SELECT * FROM customer;

When used in a DELETE or UPDATE statement, * selects all rows from the specified table. For example, the following query deletes all rows from the customer table:

DELETE FROM customer WHERE *;

However, it’s generally not recommended to use * in production code because it can have unintended consequences, such as selecting too much data or creating performance issues. It’s better to explicitly list the columns or rows that you need to select or modify in your SQL statements.

Yes, the underscore symbol (_) is a wildcard character that is used in SQL to match any single character in a string.

It is typically used in conjunction with the LIKE operator to perform pattern matching on character strings. For example, the following query selects all rows from the customer table where the last name starts with the letter “S” and the second letter is any character:

SELECT * FROM customer WHERE last_name LIKE ‘S_’;

This query would match last names such as “Smith” and “Stewart”, but not “Scott” or “Stone”.

Note that the underscore character matches exactly one character, whereas the percent sign (%) matches any number of characters (including zero).

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

A “condition” in the WHERE clause may compare inequality, like > SOME, >= SOME, < SOME, or <= SOME, between literal, column, or expression values and a lists of values returned by a subquery.

A

Yes, a “condition” in the WHERE clause may use inequality operators (> SOME, >= SOME, < SOME, or <= SOME) to compare a literal, column, or expression value with a list of values returned by a subquery. The subquery must return a single column with one or more rows, and the inequality operator compares the value on the left side with the set of values returned by the subquery.

Here’s an example that returns all the customers who have spent more than the average amount spent by all customers:

SELECT first_name, last_name, amount
FROM customer c
JOIN payment p ON c.customer_id = p.customer_id
WHERE amount > (SELECT AVG(amount) FROM payment);

In this example, the WHERE clause uses the > operator to compare the amount column of the payment table with the average amount returned by the subquery (SELECT AVG(amount) FROM payment). The JOIN clause is used to link the customer and payment tables using the customer_id column. The query returns the first name, last name, and amount spent by each customer who has spent more than the average.

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

With a tree table join, there are three tables and two join types in the FROM clause, and two on subclauses.

A

A tree table join involves multiple tables where there is a parent-child relationship between the tables. In a tree table join, there can be more than two tables involved. Each child table may have its own parent table, creating a hierarchical relationship.

A tree table join typically involves a single join type (i.e. INNER JOIN) and multiple ON subclauses. The JOIN type is used to specify the relationship between the parent and child tables, while the ON subclauses are used to specify the join conditions between the tables.

Therefore, the statement “With a tree table join, there are three tables and two join types in the FROM clause, and two on subclauses” is not accurate.

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

In relational databases, facts are broken into separate pieces (or tables) to avoid redundancy, and queries let you gather data from two or more tables by using join operations.

A

True

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

MySQL manages a self-join between two copies of the same table is through a primary key and foreign key column in the same table.

A

Yes, that’s correct. A self-join in MySQL involves joining a table to itself. To do this, you use the same table name for both the left and right tables in the join, but you give them different aliases to distinguish between them. You also use the primary key and foreign key columns in the same table to create the join condition. The primary key column represents the parent row, and the foreign key column represents the child row. For example, consider a table called “employee” that has a column called “supervisor_id” that references the “employee_id” column in the same table. You can use a self-join to find all employees and their supervisors by joining the “employee” table to itself on the “employee_id” and “supervisor_id” columns.

18
Q

A single query returns the same result as two queries joined by the union operator.

A

This statement is not always true.

A single query can often be written to achieve the same result as two queries joined by the union operator, but it depends on the specific query and data being queried. In some cases, using a union operator may be necessary to combine results from two or more queries.

The decision to use a single query or a union of multiple queries depends on the specific requirements of the query and the performance characteristics of the database being used.

19
Q

A except B result is equivalent to subtracting the rows that are in both A and B from the A set.

A

Yes, that’s correct. The EXCEPT operator is used to return all rows from one query result set that are not present in another query result set. In other words, it subtracts the rows that are in both sets (A and B) from the A set, and returns the remaining rows. This operation is also known as “set difference”.

For example, the following query returns all the cities from the cities table that are not in the big_cities table:

SELECT city_name
FROM cities
EXCEPT
SELECT city_name
FROM big_cities;

This query subtracts the rows from the second query (i.e., cities in the big_cities table) from the rows in the first query (i.e., all cities in the cities table) and returns the remaining cities that are not in the big_cities table.

20
Q

The following query id:

mysql> SELECT 1 num, ‘one’ str
-> UNION
-> SELECT 2 num, ‘two’ str
-> UNION
-> SELECT 3 num, ‘three’ str
-> UNION
-> SELECT 2 num, ‘two’ str;
It returns a result like the following:

+—–+——-+
| num | str |
+—–+——-+
| 1 | one |
| 2 | two |
| 3 | three |
+—–+——-+
3 rows in set (0.11 sec)

A

This query creates a result set that combines the four individual SELECT statements using the UNION operator. Each SELECT statement returns two columns: one column named “num” and another column named “str”. The first SELECT statement returns the values “1” and “one”, the second SELECT statement returns “2” and “two”, the third SELECT statement returns “3” and “three”, and the fourth SELECT statement returns “2” and “two”. The UNION operator combines the four result sets and removes any duplicate rows. In this case, the row with “2” and “two” is duplicated, so the final result set contains three rows with unique values for “num” and “str”.

21
Q

MySQL’s ‘INSERT()’ function takes what kinds of arguments?

A

MySQL’s INSERT() function takes three arguments:

A string to insert into.
The position in the string to start the insertion.
The number of characters to delete before inserting the new string.
The syntax for the function is INSERT(str,pos,len,newstr).

For example, SELECT INSERT(‘Hello, world!’, 8, 5, ‘MySQL’); would return the string ‘Hello, MySQL!’.

22
Q

The vi editor lets you disable line numbers with which of the following commands?

A

:set nonumber

23
Q

What data type is returned by an ascii() function?

A

The ASCII() function in MySQL returns an integer representing the ASCII code value of the leftmost character of a string. The data type returned by the function is INT.

24
Q

The count() function returns the count of column values and null values in a set.

A

The COUNT() function in MySQL returns the count of non-null values in a set. It does not include null values in the count. If you want to count null values, you can use the COUNT(*) function.

25
Q

The following query uses the COUNT function to return the first letter of the last_name column values from the customer table where the last_name column values that start with the letter ‘K’ or ‘M’ in ascending last_name order.

mysql> SELECT SUBSTR(last_name,1,2) AS first_letter
-> , last_name
-> , COUNT(*)
-> FROM customer
-> WHERE substr(last_name,1,1) IN (‘K’,’M’)
-> GROUP BY SUBSTR(last_name,1,2)
-> ORDER BY 2 DESC;

A

False

The following query uses the COUNT function to return the first two characters of the last_name column values from the customer table where the last_name column values that start with the letter ‘K’ or ‘M’ in ascending last_name order.

mysql> SELECT SUBSTR(last_name,1,2) AS first_two_letters
-> , last_name
-> , COUNT(*)
-> FROM customer
-> WHERE substr(last_name,1,1) IN (‘K’,’M’)
-> GROUP BY SUBSTR(last_name,1,2)
-> ORDER BY 2 ASC;

Note that the SUBSTR function is used to extract the first two characters of the last_name column, and the ORDER BY clause sorts the result set in ascending order of the last_name column.

26
Q

The count() function creates an explicit group when it is the only thing returned in the SELECT-list.

A

This statement is incorrect. The count() function does not create an explicit group when it is the only thing returned in the SELECT-list. Instead, it returns a single row with the count of all rows in the table or the count of rows that match the WHERE clause if specified.

For example, the following query returns the count of all rows in the customer table:

SELECT COUNT(*) FROM customer;

It will return a single row with the count of all the rows in the customer table, without creating any groups.

27
Q

The max() function returns the maximum value within a set.

A

Correct

The MAX() function in MySQL returns the maximum value in a set of values. It is often used in combination with the GROUP BY clause to find the maximum value for each group in a table. For example, the following query returns the maximum salary for each department:

SELECT department, MAX(salary) FROM employees GROUP BY department;

28
Q

Aggregate functions can work with columns and expressions as arguments.

A

Yes, aggregate functions can work with both columns and expressions as arguments. For example, you can use an aggregate function like SUM() with a column containing numeric values, or with a mathematical expression that returns a numeric value, such as SUM(price * quantity) to calculate the total cost of a set of items. Similarly, you can use MAX() or MIN() with either columns or expressions to return the maximum or minimum value, respectively, within the specified set.

29
Q

You can execute a noncorrelated subquery inside or outside of the containing query.

A

Yes, that’s correct. A noncorrelated subquery is a query that can be executed independently of the outer query and returns a single value to the outer query. It can be executed inside the SELECT, WHERE, or HAVING clauses of the containing query, or it can be executed outside of the containing query and the result can be passed to the containing query. Noncorrelated subqueries are typically used to calculate a value or condition based on a set of data in another table or based on the results of another query.

30
Q

The following shows how to fabricate a data set.

mysql> SELECT *
-> FROM (SELECT ‘Y’ AS flag
-> UNION ALL
-> SELECT ‘N’ AS flag) decision;
It will return the following result set:

+——+
| flag |
+——+
| Y |
| N |
+——+
2 rows in set (0.01 sec)

A

Yes, that is correct. The query is using a subquery to create a fabricated data set with two rows containing the values ‘Y’ and ‘N’. The outer query is then selecting all columns from this fabricated data set.

31
Q

The ALL operator returns true when it matches at least one value returned by the subquery.

A

That statement is incorrect. The ALL operator returns true if the comparison operator returns true for all values returned by the subquery. For example:

SELECT *
FROM mytable
WHERE column1 > ALL (SELECT column1 FROM myothertable);

In this example, the query returns all rows from mytable where column1 is greater than all the values returned by the subquery.

32
Q

A subquery acts like a temporary table within the containing SQL statement’s scope.

city_id | city |

A

Yes, that’s correct. A subquery is a query that is embedded within another query and acts as a temporary table within the scope of the containing SQL statement. The result set of the subquery is used by the containing SQL statement as if it were a table. The subquery can be used in a variety of clauses, including the SELECT, FROM, WHERE, HAVING, and JOIN clauses.

33
Q

A CROSS JOIN multiplies rows and adds columns to create a result set.

A

Yes, that’s correct. A CROSS JOIN, also known as a cartesian product, combines each row from the first table with every row from the second table to create a new result set. The resulting table will have a number of rows equal to the product of the number of rows in each of the original tables. If the original tables have additional columns besides the ones being joined, those columns will be included in the resulting table.

34
Q

A COUNT(*) function in the SELECT-list of a LEFT JOIN returns 1 for any row not found in the intersection of the LEFT JOIN results

A

A COUNT(*) function in the SELECT-list of a LEFT JOIN will return 1 for every row in the left table regardless of whether there is a match in the right table. If there is no match in the right table, the columns from the right table will be NULL. If you only want to count the number of rows in the left table that have a match in the right table, you should use the COUNT(column_name) function on a non-null column from the right table and include it in the WHERE clause.

35
Q

The MySQL proprietary conditional logic function is the DECODE() function offers only equality comparison.

A

Actually, MySQL does not have a built-in DECODE() function. DECODE() is a function that is available in some other database management systems, such as Oracle.

In MySQL, the equivalent function to DECODE() is the CASE statement, which allows you to evaluate a set of conditions and return a result based on the first condition that is true. The syntax for the CASE statement is:

CASE value
WHEN compare_value1 THEN result1
WHEN compare_value2 THEN result2

WHEN compare_valueN THEN resultN
ELSE default_result
END;

The CASE statement can include any comparison operator, not just equality.

36
Q

What opperators are supported by a searched CASE expression?

A

A searched CASE expression in MySQL supports comparison operators such as =, <>, !=, <, <=, >, >=, as well as logical operators such as AND, OR, and NOT.

Yes, a searched case expression supports the IN operator. The IN operator can be used to compare the expression with a set of values, like in the following example:

SELECT CASE
WHEN my_column IN (‘A’, ‘B’, ‘C’) THEN ‘Value is A, B, or C’
WHEN my_column IN (‘D’, ‘E’, ‘F’) THEN ‘Value is D, E, or F’
ELSE ‘Value is not A, B, C, D, E, or F’
END
FROM my_table;

In this example, the CASE expression checks if the value of my_column is included in the sets (‘A’, ‘B’, ‘C’) and (‘D’, ‘E’, ‘F’), and returns different values accordingly.

Yes, a searched CASE expression supports the “LIKE” operator for pattern matching. Here is an example of a searched CASE expression that uses the “LIKE” operator:

SELECT
column1,
column2,
CASE
WHEN column1 LIKE ‘%pattern%’ THEN ‘match’
ELSE ‘no match’
END AS match_status
FROM table1;

In this example, the searched CASE expression checks if column1 contains the pattern “pattern” using the “LIKE” operator. If it does, the expression returns “match”, and if it doesn’t, it returns “no match”.

37
Q

The conditional logic IF() function is built into the SQL grammar and works in the SELECT, INSERT, UPDATE, and DELETE statements.

A

Yes, that is correct. The IF() function is a built-in function in MySQL and works in various SQL statements including SELECT, INSERT, UPDATE, and DELETE statements. It allows you to perform conditional operations and return different values based on a given condition. The syntax of the IF() function is as follows:

IF(condition, value_if_true, value_if_false)

Where condition is the expression to evaluate, value_if_true is the value to return if the condition is true, and value_if_false is the value to return if the condition is false.

38
Q

The following statement lets you create a unique B-tree index of a table (the guillemet or angle brackets are only to illustrate that any table, column, or constraint name are placeholders for valid tables, indexes, columns, or constraints of a database).

mysql> CREATE UNIQUE INDEX <index_name>
-> ON <table_name> (<column_name1>, <column_name2>);</column_name2></column_name1></table_name></index_name>

A

Yes, that is correct. The statement creates a unique B-tree index on a table using one or more columns as the index key. The index ensures that the values in the indexed columns are unique, which means that no two rows can have the same combination of values in those columns. If any duplicates are found when adding or updating rows, an error is generated.

39
Q

Primary key constraints guarantee not null behaviors but not uniqueness.

A

This statement is incorrect. Primary key constraints guarantee both not null behavior and uniqueness of the values in the column or combination of columns that are defined as the primary key. This means that no two rows can have the same primary key value, and that the primary key value cannot be null. Primary keys are often used to uniquely identify each row in a table.

40
Q

The B-tree index works best with columns that have non-unique data.

A

The B-tree index works well with columns that have unique or non-unique data. However, it is particularly useful for columns that have non-unique data, as it allows for efficient retrieval of multiple rows that match a given key value.

41
Q

What are valid options for foreign key constraints?

A

When defining a foreign key constraint in MySQL, you can specify various options. Here are some of the common options:

ON DELETE: This option determines what should happen when a row in the parent table (referenced table) is deleted. The available options are CASCADE, SET NULL, RESTRICT, NO ACTION, and SET DEFAULT.

ON UPDATE: This option determines what should happen when a row in the parent table is updated. The available options are CASCADE, SET NULL, RESTRICT, NO ACTION, and SET DEFAULT.

MATCH: This option specifies how the values in the referenced columns should match the values in the referencing columns. The available options are FULL, PARTIAL, and SIMPLE.

DEFERRABLE: This option specifies whether the constraint can be deferred (i.e., checked at the end of the transaction) or not. The available options are NOT DEFERRABLE, DEFERRABLE INITIALLY IMMEDIATE, and DEFERRABLE INITIALLY DEFERRED.

ENFORCED: This option specifies whether the constraint should be enforced or not. The available options are ENFORCED and NOT ENFORCED.

Here’s an example of a foreign key constraint with some of these options:

CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
FOREIGN KEY (customer_id)
REFERENCES customers (customer_id)
ON DELETE CASCADE
ON UPDATE RESTRICT
MATCH SIMPLE
DEFERRABLE INITIALLY DEFERRED
ENFORCED
);