Databases Flashcards

1
Q

How do you remove duplicates from a field in SQL?

A

SELECT DISTINCT(fieldname)

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

How to only return a subset of results from select?

A

LIMIT

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

How would you return the 10th-15th results of a select statement?

A

LIMIT 9,5

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

How do you sort by more than one column?

A

ORDER BY field1, field2, field3

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

How to do >= x or <= y?

A

BETWEEN x and y

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

How should you use quotes?

A

single quotes for text column names like ‘favorite food’

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

What do you need to escape in column names?

A

Single quotes need an extra quote before them

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

What are the logical operators?

A

AND, OR, IN, NOT

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

How to combine two fields into one?

A

CONCAT(thing,thing,thing)

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

How to rename a result?

A

SELECT thing AS other_thing

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

What arithmetic operations does it support?

A

+ * /

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

What are the LIKE metacharacers?

A

_ matches a single character, % matches 0 or more

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

How can you join without using JOIN?

A

Select T1.thing, T2.thing from T1,T2 WHERE T1.key = T2.key;

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

What are the types of JOIN? Explain each. Which is used when just JOIN is specified?

A

Inner join (default)- intersection of two tables, use ON keyword
Left (Outer) Join - Returns all from left table, even if there are no matches in right
Right (Outer) Join - Opposite of left join

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

How to combine rows from two tables in to one? How to do it by removing duplicates? What if the columns don’t’ match exactly?

A

select rows from T1 U selet rows from T2. U is UNION ALL, and UNION removes duplicates. Use a null for one set if it doesn’t have the field from another set.

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

How to add a row?

A

INSERT INTO table VALUES (a,b,c,d)

17
Q

How to add a row with only specific fields?

A

INSERT INTO table (col_a, col_b, col_d) VALUES (a, b, d)

18
Q

How to modify a row?

A

UPDATE table SET col_a=value,col_b=val WHERE condition; Without WHERE all rows get updated.

19
Q

How to create a table?

A

CREATE TABLE name (col_name data_type(size), col_name data_type(size));

20
Q

How to make a Primary Key?

A

when setting up the field add NOT NULL and AUTOINCREMENT after the data_type, and after a coma put PRIMARY KEY(fieldname)

21
Q

What are the constraints you can put on fields?

A

NOT NULL, UNIQUE, CHECK (uses a logical expression to validate), DEFAULT, PRIMARY KEY

22
Q

How to add a column to a table?

A

ALTER/MODIFY TABLE name

ADD col_name data_type

23
Q

How to remove a column from a table?

A

ALTER/MODIFY TABLE name

drop colname;

24
Q

How to delete a table?

A

Drop tablename;

25
Q

How would you rename a column?

A

ALTER TABLE name CHANGE oldname newname datatype(size);

26
Q

How would you rename a table?

A

RENAME TABLE old TO new;

27
Q

What’s a view? How do you create one? How do you use it later?

A

A virtual table, basically a saved selection.

CREATE VIEW name AS select_statement_here

Then can use as SELECT * FROM view;

28
Q

How would you update an existing view?

A

CREATE OR REPLACE VIEW name AS new_select_statement;

29
Q

How would you delete a view?

A

DROP VIEW name;