SQL Basics Flashcards

1
Q

What is a table?

A

a collection of related data entries and it consists of rows and columns

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

data is stored in database objects called?

A

tables

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

every table is broken up into smaller entities called?

A

fields

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

What is a field?

A

a column in a table designed to maintain specific information about every record in the table

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

What is a record?

A

a row / each individual entry that exists in a table

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

CRUD operations

A

CREATE
READ
UPDATE
DELETE

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

which SQL clause extracts data from a database

A

SELECT

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

which SQL clause updates data in a database

A

UPDATE

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

which SQL clause deletes data in a database

A

DELETE

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

which sql clause inserts new data into a database

A

INSERT INTO

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

which sql clause creates a new database

A

CREATE DATABASE

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

which sql clause modifies a database

A

ALTER DATABASE

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

which sql clause creates a new table

A

CREATE TABLE

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

which sql clause modifies a table

A

ALTER TABLE

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

which sql clause deletes a table

A

DROP TABLE

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

which sql clause creates an index (search key)

A

CREATE INDEX

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

which sql clause deletes an index

A

DROP INDEX

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

which sql statement returns only distinct (different) values

A

SELECT DISTINCT

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

how can we return the number of different countries (example)

A

SELECT COUNT(DISTINCT Country)

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

how do we filter records

A

with the WHERE clause and extracts only records that fulfill a specified condition

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

is the WHERE clause only used in SELECT statements?

A

NO! Also used in UPDATE, DELETE, etc.

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

what clause is used to specify between a certain range

A

BETWEEN

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

which clause is used to search for a pattern

A

LIKE

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

operator not equal

A

<>

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

how do we sort results in ASC or DESC order

A

ORDER BY

ASC is default

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

what operator displays a record if all conditions are TRUE

A

AND

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

what operator displays a record if any of the conditions are TRUE

A

OR

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

what operator is used to give the opposite result called the negative result

A

NOT

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

INSERT INTO table_name (column1, column2, …)
VALUES (value1, value2, …)

30
Q

a field with a NULL value is a field with __ __

31
Q

how to check for null values with operators

A

IS NULL | IS NOT NULL

32
Q

UPDATE table_name
SET column1 = value1, column2 = value2, …
WHERE condition;

A

if you omit the WHERE clause, all records will be updated

33
Q

DELETE FROM table_name WHERE condition;

A

where clause specifies which record(s) should be deleted and if you forget this clause all records in the table will be deleted!

34
Q

what if we want to specify number of records to return

A

MySQL supports LIMIT clause

some support SELECT TOP # * FROM table_name;

Oracle uses FETCH FIRST n ROWS ONLY and ROWNUM (after FROM or ORDER BY)

35
Q

what do aggregate functions do?

A

a function that performs a calculation on a set of values and returns a single value

36
Q

what SQL clause are aggregate functions often used with

A

GROUP BY (aggegate functions can be used to return a single value for each group)

37
Q

GROUP BY clause does what?

A

splits the result-set into groups of values

38
Q

what aggregate function does NOT ignore null values

39
Q

SQL aggregate function examples

A

MIN, MAX, COUNT, SUM, AVG

40
Q

what does MIN() return

A

smallest value of selected column

41
Q

what does MAX() return

A

largest value of selected column

42
Q

if you specify a column name instead of * with COUNT(), will NULL values be counted?

43
Q

what does SUM() return?

A

the total sum of a numeric column

44
Q

can also use SUM() as an expression

A

SELECT SUM(Quantity * 10)
FROM OrderDetails;

45
Q

what does AVG() return?

A

the average value of the numeric column

NULL values are ignored

46
Q

concatenate columns

A

SELECT CustomerName, Address + ‘, ‘ + PostalCode + ‘ ‘ + City + ‘, ‘ + Country AS Address
FROM Customers;

47
Q

concatenate columns in MySQL

A

SELECT CustomerName, CONCAT(Address, ‘, ‘, PostalCode, ‘, ‘, City, ‘, ‘, Country) AS Address
FROM Customers;

48
Q

concatenate columns in Oracle

A

SELECT CustomerName, (Address || ‘, ‘ || PostalCode || ‘ ‘ || City || ‘, ‘ || Country) AS Address
FROM Customers;

49
Q

we want to combine rows from two or more tables based on a related column between them

50
Q

different types of SQL Joins

A

(INNER) JOIN
LEFT (OUTER) JOIN
RIGHT (OUTER) JOIN
FULL (OUTER) JOIN

51
Q

(INNER) JOIN

A

returns records that have matching values in both tables

52
Q

LEFT (OUTER) JOIN

A

returns all records from left table, and matched records from right table

53
Q

RIGHT (OUTER) JOIN

A

returns all records from right table, and matched records from left table

54
Q

FULL (OUTER) JOIN

A

returns all matching records from both tables where the other table matches or not

55
Q

SQL Self Join

A

a regular join but the table is joined with itself

56
Q

what operator can we use to combine the result-set of two or more SELECT statements

A

UNION

but every SELECT statement within UNION must have same number of columns and similar data types and must be in same order

57
Q

what is different between UNION and UNION ALL?

A

UNION ALL allows for duplicate values

58
Q

what if we want to filter rows after they have been aggregated?

A

use HAVING clause

59
Q

EXISTS operator

A

test for existence of any record in a subquery

60
Q

ANY and ALL operators allow you to perform a comparison between a ___ ___ value and a ____ of other values

A

single column, range

61
Q

ANY Operator returns

A

boolean value as result

so TRUE if ANY of subquery values meet the condition

62
Q

ALL operator returns

A

a boolean value as a result

returns TRUE if ALL of the subquery values meet the condition

63
Q

SELECT INTO statement does what

A

copies data from one table into a new table

SELECT *
INTO newTable [IN externalDb]
FROM oldTable
WHERE condition;

64
Q

SQL case expression goes through conditions and returns a value when first condition is met

A

CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END AS columnAlias;

65
Q

MySQL -> IFNULL() and COALESCE()

A

allows us to return an alternative value if an expression is NULL

66
Q

what is a stored procedure?

A

a prepared SQL code that we can save so code can be reused over and over again

67
Q

how do we execute a stored procedure?

A

call it

EXEC procedure_name;

68
Q

can we pass parameters to a stored procedure so that stored procedure can act based on parameter value(s) that is passed?

69
Q

CREATE PROCEDURE procedure_name
AS sql_statement
GO;

A

CREATE PROCEDURE SelectAllCustomers
AS
SELECT * FROM Customers
GO;

70
Q

stored procedure example with one parameter

A

CREATE PROCEDURE SelectAllCustomers @City nvarchar(30)
AS
SELECT * FROM Customers WHERE City = @City
GO;

EXEC SelectAllCustomers @City = ‘London’;

71
Q

SQL comments

A

– single line comment

/* Multi-
line
comment */