SQL-02 Flashcards

1
Q

What is an SQL parser?

A

An SQL parser is a tool or component of a database management system that reads and interprets SQL queries. It breaks down the queries into understandable segments for further processing.

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

What role does an SQL parser play in the execution of an SQL query?

A

The SQL parser’s primary role is to parse the query, which involves checking syntax, validating SQL commands, and organizing the parts of the query into a structured format for the database to execute.

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

How does an SQL parser analyze the syntax of a query?

A

The parser performs syntax analysis to ensure the query complies with SQL grammar rules. It checks for proper use of keywords, expressions, operators, and overall query structure.

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

What is the OFFSET clause in SQL?

A

The OFFSET clause in SQL is used to skip a specified number of rows before starting to return rows from the query. It’s often used with the LIMIT clause for pagination purposes.

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

How is the OFFSET clause typically used in an SQL query?

A

The basic syntax is SELECT * FROM table LIMIT x OFFSET y;, where x is the number of rows to return, and y is the number of rows to skip.

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

How does using OFFSET affect query performance?

A

While OFFSET is useful for pagination, it can lead to performance issues on large tables, as the database still reads through all the preceding rows before returning the result.

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

Is there an alternative to using OFFSET in newer SQL versions?

A

Yes, newer SQL standards and some databases offer the FETCH NEXT syntax, which can be more readable and aligns with the latest SQL standards. For example, SELECT * FROM table LIMIT x FETCH NEXT y ROWS ONLY;.

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

Does the implementation of OFFSET vary across different SQL databases?

A

Yes, while the basic concept of OFFSET is similar, the exact syntax and capabilities may vary slightly between different SQL database systems like MySQL, PostgreSQL, and SQL Server.

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

Can you perform conditional calculations in SQL?

A

Yes, use the CASE statement for conditional logic in calculations. For example, SELECT CASE WHEN column1 > 100 THEN column1 * 0.9 ELSE column1 * 1.1 END AS adjusted_value FROM table; adjusts values based on a condition.

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

How important is the order of operations in SQL calculations?

A

Very important. SQL follows the standard mathematical order of operations (parentheses, exponents, multiplication and division, addition and subtraction). Always use parentheses to ensure the correct order of calculations.

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

How do you perform basic arithmetic operations in an SQL SELECT statement?

A

You can perform arithmetic operations like addition, subtraction, multiplication, and division directly on column values. For example, SELECT column1 + column2 AS total FROM table; calculates the sum of two columns.

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

Can you use mathematical functions in inline calculations in SQL?

A

Yes, SQL supports various mathematical functions like ROUND, CEILING, FLOOR, ABS, etc., which can be used in SELECT statements. For example, SELECT ROUND(column1, 2) FROM table; rounds the values in column1 to 2 decimal places.

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

How do you extract a substring from a string in SQL?

A

Use the SUBSTRING function. The syntax is SUBSTRING(string FROM start FOR length), where start is the starting position and length is the number of characters to extract.

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

How can you change the case of a specific substring within a string in SQL?

A

Combine SUBSTRING with UPPER or LOWER. For example, SELECT SUBSTRING(UPPER(column_name) FROM 1 FOR 3) FROM table; extracts the first three characters of column_name and converts them to upper case.

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

What should you consider when using string functions with columns that may contain NULL values?

A

Be aware that string functions will return NULL if the input is NULL. You can use COALESCE or IFNULL to handle NULL values, e.g., SELECT UPPER(COALESCE(column_name, ‘default’)) FROM table;.

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

change case with substring

A

select * , concat(lower(substring(customer_first_name, 1 ,1)),
upper(substring(customer_first_name, 2))) as first_name
from farmers_market.customer;

17
Q

Do NULL and blank values have different storage space requirements?

A

Yes. NULL values usually take up less space than blank strings, as they represent the absence of data, whereas blank strings still require storage for the string data type.

18
Q

How do NULL and blank values affect default column values in SQL?

A

If a column has a default value, inserting NULL (explicitly or by omission) will store NULL, but inserting an empty string will store the empty string, not the default value.

19
Q

What is the difference between inserting NULL and a blank value into a database?

A

Inserting NULL means you’re inserting a lack of value. Inserting a blank value (‘’) means you’re inserting an empty string, which is a specific value.

20
Q

How are NULL values handled in SQL aggregate functions?

A

NULL values are generally ignored by aggregate functions like SUM, AVG, COUNT, etc. For instance, COUNT(column) counts only non-NULL values.

21
Q

How do blank values behave in comparisons in SQL?

A

Blank values are treated like any other string value. For example, column = ‘’ checks if the column contains an empty string.

22
Q

How do NULL values behave in comparisons in SQL?

A

In SQL, any comparison with NULL (like column = NULL or column != NULL) results in NULL, which is considered false in a WHERE clause. To check for NULL, use IS NULL or IS NOT NULL.

23
Q

What is a blank or empty string in SQL?

A

A blank or empty string is a string value with no characters, denoted as ‘’. It is a known value, indicating that the string is empty but not missing.

24
Q

What does NULL represent in SQL?

A

NULL in SQL represents the absence of a value or an unknown value. It’s a marker indicating that the value is missing or not applicable, not even an empty string.

25
Q

What is a correlated subquery in SQL?

A

A correlated subquery is a subquery that references one or more columns in the outer SQL query. It is evaluated repeatedly, once for each row processed by the outer query.

26
Q

Can you provide a basic example of a correlated subquery?

A

Sure, consider SELECT * FROM table1 t1 WHERE EXISTS (SELECT * FROM table2 t2 WHERE t1.id = t2.id);. Here, the subquery depends on the outer query’s t1.id.

27
Q

How do correlated subqueries affect performance?

A

Correlated subqueries can be slower than non-correlated subqueries because they must be evaluated multiple times – once for each row in the outer query.

28
Q

In what situations are correlated subqueries used?

A

They are often used in cases where we need to compare or evaluate data in the same table or in different tables based on row-by-row comparisons.

29
Q

How are aggregate functions used in correlated subqueries?

A

An aggregate function in a correlated subquery can operate on values from the outer query. For example, you can select rows from a table where a column value is greater than the average value in the same table.

30
Q

What is the difference between EXISTS and IN clauses in correlated subqueries?

A

EXISTS checks for the existence of rows returned by the subquery and is true if the subquery returns any row, while IN compares a column against a list of values returned by the subquery.

31
Q

Can correlated subqueries be used in UPDATE statements?

A

Yes, you can use a correlated subquery in an UPDATE statement to update rows based on values in the same table or another table.

32
Q

How are correlated subqueries used in DELETE statements?

A

In a DELETE statement, a correlated subquery can identify rows to be deleted based on conditions related to another table or the same table.