2. SELECT Query Flashcards

(32 cards)

1
Q

Single line comment in SQL Server

A

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

In an SQL statement, reserved words like
* clauses (SELECT, FROM, WHERE)
* built-in functions (LOWER)
* data types (VARCHAR)

are called _ .

A

keywords

Keywords form the syntax of SQL.

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

In an SQL statement, identifiers are _

A

names of column, table or schema.

SELECT name, LOWER(country)
FROM customers
WHERE country = "India"

Here, name, country, customer are identifiers.

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

In an SQL statement, the _ are used inside WHERE clause to compare values.

A

operators

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

An SQL statement is composed of clauses and each clause is composed of _

A

keywords, identifiers, operators and string literals.

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

In SQL statement, _ clause is used to filter columns of the table and _ clause is used to filter rows of the table.

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

List down some (as many as you can recall) clauses used in a SELECT query

A
  • SELECT
  • DISTINCT
  • TOP
  • FROM
  • WHERE
  • JOIN
  • GROUP BY
  • ORDER BY
  • HAVING
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The typical order of execution for major SQL clauses is:
* SELECT
* DISTINCT
* OFFSET / FETCH (pagination) / TOP
* FROM
* WHERE
* JOIN
* GROUP BY
* ORDER BY
* HAVING

A
  1. FROM
  2. JOIN
  3. WHERE
  4. GROUP BY
  5. HAVING
  6. SELECT
  7. DISTINCT (applied after SELECT)
  8. ORDER BY
  9. OFFSET / FETCH (pagination) / TOP
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

SQL command to specify database in advance

A

USE databasename;

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

In the SQL query:

SELECT
first_name,
score
FROM customers
WHERE country = 'Germany';

the order of execution of clauses is

A
  1. FROM
  2. WHERE
  3. SELECT
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

select all customers where score is NULL (unknown).

A
SELECT * from customers
WHERE score IS NULL;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which clause is used to sort the data?

A

ORDER BY <col-name>

By default, ordering sequence is ascending (ASC).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
INSERT INTO customers(id,first_name, country, score) 
    VALUES (1, 'Maria', 'Germany', 350),
        (2, 'John', 'USA', 900),
        (3, 'Georg', 'UK', 750),
        (4, 'Martin', 'Germany', 500),
        (5, 'Peter', 'USA', 0),
        (6, 'Sahil', 'India', null),
        (7, 'Jonney', null, 600);

SELECT * 
FROM customers
order by country DESC;

Where will ‘Jonney’ be placed in the rows of result set?

A

last row

SQL treats NULL as the lowest possible value when sorting.

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

Write query for customers table to retrieve data ordered by ascending countries and within each country ordered by descending scores.

Assume country column has repetitive entries.

A
SELECT *
FROM customers
ORDER BY
country ASC,
score DESC

This is known as multi-level (or nested) sorting in SQL

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

Write query for customers table to retrieve data ordered by ascending countries and within each country ordered by descending scores.
Ensure null countries are skipped.

Assume country column has repetitive entries.

A
SELECT *
FROM customers
WHERE country IS NOT NULL
ORDER BY country ASC, score DESC
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. SQL places NULL column as the _ row value when sorting the column ascending.
  2. SQL places NULL column as the _ row value when sorting the column descending.

(last/first)

We have some null entries in column A and we are sorting (order by) column A

A
  1. first
  2. last

SQL treats NULL as the lowest possible value when sorting.

17
Q
  1. When calculating avg(), SQL _ NULL values.

(counts/ignores)

  1. If score column has entries: 600, NULL, then avg(score) will be _ .
  2. If score column has entries: NULL, NULL, then avg(score) will be _ .
A
  1. ignores
  2. 600
  3. NULL
18
Q

Find avg score and total number of customers for each country.

CREATE TABLE customers (
  id int,
  first_name varchar(15),
  country varchar(15),
  score int
);
A
select 
country,
avg(coalesce(score, 0)) as avg_score_per_country,
count(*) as count_per_country
from customers
where country is not null
group by country
19
Q

Write query to place NULL values in the end, while sorting on basis of score.

Hint: Maximum score value is 1000

A
select *, coalesce(score, 1001) as nscore
from customers
order by nscore asc;
20
Q

Retrieve customers with non-null countries,
sorted by country in descending order.

A
SELECT * 
FROM customers
where country is not null
order by country DESC;
21
Q

Which SQL clause helps aggregate data of a column by another column?

A

GROUP BY

it conveys the “for each” semantics.

For e.g., find total score for each country

22
Q

Write SQL query to find the total score values for each country.

A

SELECT country, SUM(score)
FROM customers
GROUP BY country

23
Q

If GROUP BY clause has two columns, then the _ is used as basis of aggregation.

A

combined value of two columns

24
Q

Find total score and total number of customers for each country.

A
SELECT SUM(score) AS total-score, COUNT(*) AS total_customers
FROM customers
GROUP BY country;
25
Which SQL clause is used to filter data after aggregation?
HAVING HAVING clause is executed after GROUP BY clause.
26
Write SQL query to find total score values for each country. Avoid null values for countries.
SELECT country, SUM(score) FROM customers WHERE country IS NOT NULL GROUP BY country
27
Write SQL query to find the average score values for each country where average score is more than 449.
SELECT country, AVERAGE(score) AS avg_score FROM customers GROUP BY country HAVING AVERAGE(score) > 449
28
NULLs are treated as _ (equal/unequal) when evaluating distinctness in DISTINCT clause
equal
29
Retrieve top 3 customers with highest scores.
SELECT TOP(3) * FROM customers ORDER BY scores DESC
30
Any comparison operator like <, =, > with NULL operand returns _ (false/unknown).
unknown
31
Find total number of times each customer made orders. `MyOrders: IrderId, CustomerId, Sales` ## Footnote Assume a customer can make multiple orders.
``` select CustomerID, count(*) as OrdersPerCustomer from MyOrders group by CustomerID; ```
32
`avg` ignores _ values if at least one row is non-null.
null