Structured Query Language Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is a Derived Column?

A

A new column that is a manipulation of the existing columns in your database.

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

What are the three essentials ideas that are aimed at database normalisation?

A

1 - Are the tables storing logical groupings of the data?

2 - Can I make changes in a single location, rather than in many tables for the same information?

3 - Can I access and manipulate data quickly and efficiently?

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

What are the Primary Keys and Foreign Keys?

A

A primary key is a unique column in a particular table. This is the first column in each of our tables. Here, those columns are all called id, but that doesn’t necessarily have to be the name.

A foreign key is a column in one table that is a primary key in a different table.

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

In a Left Join statement, which table is considered as the Left table

A

In a query like this, the Left Table is table_a

SELECT *
FROM table_a a
LEFT JOIN table_b b
ON a.id=b.fk

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

What is the result of the use go a full cutter join?

A

This will return the inner join result set, as well as any unmatched rows from either of the two tables being joined.

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

How to filter rows before combining tables?

A
Using a logic operator in the ON clause:
SELECT orders.*, accounts.*
FROM orders
 LEFT JOIN account
  ON order.account_id = account.id
  AND account.sales_rep_id = 321500
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When using Count(*), what is the only kind of row the won’t show up?

A

Only rows that have only NULL values.

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

What is a down side of using DISTINCT?

A

Particularly in aggregations, DISTINCT can slow you queries down quite a bit.

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

When using multiple WHEN clause, which of them will sql check if its true first?

A

The first. See example:

SELECT 
account_id, 
occurred_at, 
total,
CASE 
WHEN total > 500 THEN 'Over 500'
WHEN TOTAL > 300 THEN '301 - 500'
WHEN total > 200 THEN '201 - 300'
ELSE '200 or under' END AS total_group
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a CTE (Common Table Expression)?

A

These expressions serve the exact same purpose as subqueries that are defined once and previous to your query. This way, the query that defines your subquery has to be executed only on time.

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

What is the purpose of the LEFT function?

A

Pull character of the left side of the string and present them as a separate string

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

What is another way of using the CAST function?

A

Using ‘::’

SELECT columnA::date

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

It is appropriate to use Right, Left and SUBTR along with CAST?

A

No, because LEFT, RIGHT or SUBSTRING automatically cast data to a string data type

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

What is the purpose of the function COALESCE?

A

Returns the first NON-NULL value passed for each row

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

Explain the purpose of the Window Functions by defining the clauses in this query:
SELECT
depname,
empno,
salary,
avg(salary) OVER (PARTITION BY depname)
FROM empsalary;

A

A window function call always contains an OVER clause directly following the window function’s name and argument(s). This is what syntactically distinguishes it from a regular function or aggregate function. The OVER clause determines exactly how the rows of the query are split up for processing by the window function. The PARTITION BY list within OVER specifies dividing the rows into groups, or partitions, that share the same values of the PARTITION BY expression(s). For each row, the window function is computed across the rows that fall into the same partition as the current row.

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

What is happening when you omit the ORDER BY clause when doing aggregates with window functions?

A

There are situations where the ordering of the rows to be processed by the window function matters. Aggregation of data that follows a chronological sequence is one of these situations. If there’s no explicit information what is the criteria to order the processing, the window function will only obey the partition by clause if it exists.

17
Q

How should you use the Window Function NTILE()

A

You can use it to identify which percentile (or quartile, or any other subdivision) a given row falls into.

he syntax is NTILE(# of buckets). In this case, ORDER BY determines which column to use to determine the quartiles (or whatever number of ‘tiles you specify).

18
Q

What are the top 3 things easily under you control that will affect the number of calculations of query and as consequence its performance?

A
  • Table Size
  • The use of JOINS
  • Aggregations
19
Q

What are 2 examples of things that are not under your control that will affect the performance of your query/

A
  • Other users running queries concurrently on the same database
  • Database software and optimization (Postgre is optize3d differently then Redshift)