SQL Basics Flashcards

(71 cards)

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
how do we sort results in ASC or DESC order
ORDER BY ASC is default
26
what operator displays a record if all conditions are TRUE
AND
27
what operator displays a record if any of the conditions are TRUE
OR
28
what operator is used to give the opposite result called the negative result
NOT
29
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...)
30
a field with a NULL value is a field with __ __
no value
31
how to check for null values with operators
IS NULL | IS NOT NULL
32
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
if you omit the WHERE clause, all records will be updated
33
DELETE FROM table_name WHERE condition;
where clause specifies which record(s) should be deleted and if you forget this clause all records in the table will be deleted!
34
what if we want to specify number of records to return
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
what do aggregate functions do?
a function that performs a calculation on a set of values and returns a single value
36
what SQL clause are aggregate functions often used with
GROUP BY (aggegate functions can be used to return a single value for each group)
37
GROUP BY clause does what?
splits the result-set into groups of values
38
what aggregate function does NOT ignore null values
COUNT()
39
SQL aggregate function examples
MIN, MAX, COUNT, SUM, AVG
40
what does MIN() return
smallest value of selected column
41
what does MAX() return
largest value of selected column
42
if you specify a column name instead of * with COUNT(), will NULL values be counted?
NO
43
what does SUM() return?
the total sum of a numeric column
44
can also use SUM() as an expression
SELECT SUM(Quantity * 10) FROM OrderDetails;
45
what does AVG() return?
the average value of the numeric column NULL values are ignored
46
concatenate columns
SELECT CustomerName, Address + ', ' + PostalCode + ' ' + City + ', ' + Country AS Address FROM Customers;
47
concatenate columns in MySQL
SELECT CustomerName, CONCAT(Address, ', ', PostalCode, ', ', City, ', ', Country) AS Address FROM Customers;
48
concatenate columns in Oracle
SELECT CustomerName, (Address || ', ' || PostalCode || ' ' || City || ', ' || Country) AS Address FROM Customers;
49
we want to combine rows from two or more tables based on a related column between them
use JOINs
50
different types of SQL Joins
(INNER) JOIN LEFT (OUTER) JOIN RIGHT (OUTER) JOIN FULL (OUTER) JOIN
51
(INNER) JOIN
returns records that have matching values in both tables
52
LEFT (OUTER) JOIN
returns all records from left table, and matched records from right table
53
RIGHT (OUTER) JOIN
returns all records from right table, and matched records from left table
54
FULL (OUTER) JOIN
returns all matching records from both tables where the other table matches or not
55
SQL Self Join
a regular join but the table is joined with itself
56
what operator can we use to combine the result-set of two or more SELECT statements
UNION but every SELECT statement within UNION must have same number of columns and similar data types and must be in same order
57
what is different between UNION and UNION ALL?
UNION ALL allows for duplicate values
58
what if we want to filter rows after they have been aggregated?
use HAVING clause
59
EXISTS operator
test for existence of any record in a subquery
60
ANY and ALL operators allow you to perform a comparison between a ___ ___ value and a ____ of other values
single column, range
61
ANY Operator returns
boolean value as result so TRUE if ANY of subquery values meet the condition
62
ALL operator returns
a boolean value as a result returns TRUE if ALL of the subquery values meet the condition
63
SELECT INTO statement does what
copies data from one table into a new table SELECT * INTO newTable [IN externalDb] FROM oldTable WHERE condition;
64
SQL case expression goes through conditions and returns a value when first condition is met
CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 WHEN conditionN THEN resultN ELSE result END AS columnAlias;
65
MySQL -> IFNULL() and COALESCE()
allows us to return an alternative value if an expression is NULL
66
what is a stored procedure?
a prepared SQL code that we can save so code can be reused over and over again
67
how do we execute a stored procedure?
call it EXEC procedure_name;
68
can we pass parameters to a stored procedure so that stored procedure can act based on parameter value(s) that is passed?
yes
69
CREATE PROCEDURE procedure_name AS sql_statement GO;
CREATE PROCEDURE SelectAllCustomers AS SELECT * FROM Customers GO;
70
stored procedure example with one parameter
CREATE PROCEDURE SelectAllCustomers @City nvarchar(30) AS SELECT * FROM Customers WHERE City = @City GO; EXEC SelectAllCustomers @City = 'London';
71
SQL comments
-- single line comment /* Multi- line comment */