SQL Flashcards

(34 cards)

1
Q

UPDATE statement

A

UPDATE {table} SET {field}={value} WHERE {field}={value}

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

Make new table

A

CREATE TABLE {table} (
{col1 def},
{col2 def}
);

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

New table (foreign key)

A

CONSTRAINT {colname} FOREIGN KEY {local_fk_colname} REFERENCES {table}({column}),

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

New table (primary key; auto; not null) [sqlite]

A

{colname} INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,

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

New table (primary key; auto; not null) [postgres]

A

{colname} SERIAL PRIMARY KEY NOT NULL,

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

New table (column definition)

A

{colname} {type} [NOT NULL],

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

remove table (test for existence first)

A

DROP TABLE IF EXISTS {table};

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

New index

A

CREATE INDEX {idx_name} ON {table}({column});

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

INSERT statement

A

INSERT INTO {table} ({col1}, {col2}) VALUES ({val1}, {val2});

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

COUNT statement

A

SELECT COUNT({col})
FROM {table}
WHERE {condition};

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

SELECT statement

A

SELECT {col1}, {col2}
FROM {table};

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

INNER JOIN

A

SELECT {table1}.{col1}, {table2}.{col2}
FROM {table1}
INNER JOIN {table2} ON {table1}.{col}={table2}.{col};

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

UNION statement

A

SELECT {cols} FROM {table1}
UNION
SELECT {cols} FROM {table2};

(cols must be same number and type)

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

DELETE statement

A

DELETE FROM {table} WHERE {condition};

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

SUM statement

A

SELECT SUM({col})
FROM {table}
WHERE {condition};

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

AVG statement

A

SELECT SUM({col})
FROM {table}
WHERE {condition};

17
Q

sqlite list tables

18
Q

sqlite show DB schema

A

.schema {table}

19
Q

postgres choose database

A

bash# pgsql {db_name}

pgsql> \c {db_name}

20
Q

postgres list tables

21
Q

postgres show table schema

A

psql> \d+ {table}

22
Q

Search for string in text field

A

SELECT * FROM {table}
WHERE {col} LIKE ‘%word1%’

23
Q

postgres list databases

24
Q

subquery syntax

A

SELECT
first_name,
salary
FROM
employees
WHERE
salary = (
SELECT
MAX(salary)
FROM
employees
);

25
Used with aggregate functions (e.g. COUNT, SUM) to combine results set
GROUP BY
26
Get records that fulfill a specified condition
WHERE
27
Get records that fulfill a specified condition (for aggregates)
HAVING
28
Aggregate Functions
MIN, MAX, COUNT, SUM, AVG
29
Create a view
CREATE VIEW {view_name} AS SELECT {cols} FROM {table} WHERE {condition};
30
Add column to table
ALTER TABLE {table} ADD {col_name} {type};
31
Remove column from table
ALTER TABLE {table} DROP COLUMN {col_name};
32
Rename a column
ALTER TABLE {table} RENAME COLUMN {old_name} TO {new_name};
33
Change column data type [postgres]
ALTER TABLE {table} ALTER COLUMN {col} TYPE {data_type};
34
Define a join table (many-to-many)
CREATE TABLE a_b ( a_id INT, b_id INT, PRIMARY KEY (a_id, b_id), FOREIGN KEY (a_id) REFERENCES A(a_id), FOREIGN KEY (b_id) REFERENCES B(b_id) );