SQL Syntax Flashcards

1
Q

The different parts of a SQL statement

A

A clause

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

Basic 2 elements to get data from a database

A

SELECT column1, column2,

FROM table_name;

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

Select all the DIFFERENT values from the Country column in the Customers table

A

SELECT DISTINCT Country

FROM Customers;

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

The number of different (distinct) customer countries (different countries in the customer table)

A
SELECT COUNT(DISTINCT Country)
FROM Customers;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Conditions

All records where City Column has value “Berlin”

A

SELECT * FROM Customers

WHERE City = ‘Berlin’;

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

Select all records where City is NOT “Berlin”

A

SELECT * FROM Customers

WHERE NOT City = ‘Berlin’;

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

Sort result set in ascending or descending order

A

SELECT column1, column2
FROM table_name
ORDER BY column1, column2 DESC;

Ascending order by default

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

Test for NULL values

A

SELECT column_names
FROM table_name
WHERE column_name IS NULL;

or IS NOT NULL;

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

Specify a number of records to return

A

SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;

This varies greatly across DBMS, including LIMIT, FETCH FIRST, and ROWNUM

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

What are the most popular DBMS?

A

MySQL, Oracle, Microsoft SQL Server, PostgreSQL, SQLite, MongoDB

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

Smallest or largest value in a selected column

A

SELECT MIN(column_name)
FROM table_name
WHERE condition;

SELECT MAX(column_name)
FROM table_name
WHERE condition;

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

What are some functions and where would you find them?

A
AVG(), SUM(), COUNT(), MIN(), MAX()
Put them in the select clause
SELECT SUM(Quantity)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Used in the WHERE clause to search for a specified pattern in a column
Give an example

A

Like
WHERE CustomerName LIKE ‘a%’
Will find any values that start with “a”

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

Multiple values in a WHERE clause

A

IN
SELECT * FROM Customers
WHERE Country IN (‘Germany’, ‘France’, ‘UK’);

Also works with NOT IN

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

Selects values within a given range

A

SELECT * FROM Products

WHERE Price NOT BETWEEN 10 AND 20;

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

How do you create an alias to make column names more readable

A

AS
SELECT CustomerID AS ID, CustomerName AS Customer
FROM Customers;

17
Q

What is the syntax for an inner join

A

ON

SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;

18
Q

What is the syntax for left join

A

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;