SQL Flashcards

1
Q

Get all records from a table

A

SELECT * FROM table;

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

Get specific fields from a table

A

SELECT field1,field2 FROM table;

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

Get only different values from a table

A

SELECT DISTINCT field FROM table;

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

Get records with a field equal to a string value and another equal to a numeric value

A

SELECT * FROM table WHERE field1=’value’ AND field2=1;

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

Get all records from a table sorted by two fields, ascending

A

SELECT * FROM table ORDER BY field1,field2;

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

Get all records from a table sorted by a field, descending

A

SELECT * FROM table ORDER BY field DESC;

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

Add a record to a table by filling all columns

A

INSERT INTO table VALUES (value1, value2, …);

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

Add a record to a table by filling some columns

A

INSERT INTO table (field1, field2) VALUES (value1, value2);

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

Change values for records conditionally

A

UPDATE table SET field1=value1,field2=value2 WHERE condition;

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

Remove records conditionally

A

DELETE FROM table WHERE condition;

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

Get a certain number of records

A

SELECT TOP count field1,field2 FROM table;

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

Get a certain percentage of records

A

SELECT TOP pcnt PERCENT field1,field2 FROM table;

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

Get records with field1 starting with ‘happy’

A

SELECT * FROM table WHERE field1 LIKE ‘happy%’;

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

Get records with field1 not ending with ‘happy’

A

SELECT * FROM table WHERE field1 NOT LIKE ‘%happy’

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

Pattern for many unknown characters

A

%

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

Pattern for single unknown character

A

_

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

Pattern for character set

A

[ab-d]

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

Pattern for NOT character set

A

[!abc]

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

Get all records with field1 equal to any values in a set

A

SELECT * FROM table WHERE field1 IN (‘a’,’b’);

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

Get all records with field1 alphabetically between two strings

A

SELECT * FROM table WHERE field1 BETWEEN ‘astring’ AND ‘bstring’;

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

Create a short name for a table

A

SELECT o.Field1, c.Field2 FROM table1 as o, table2 as c WHERE condition;

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

Get associated records from two tables where there is at least one match in both

A

SELECT t1.f1,t2.f2 FROM t1 INNER JOIN t2 ON t1.f=t2.f

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

Get associated records from two tables showing all records from first table

A

SELECT t1.f1,t2.f2 FROM t1 LEFT JOIN t2 ON t1.f1=t2.f2

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

Get associated records from two tables showing all records from second table

A

SELECT t1.f1,t2.f2 FROM t1 RIGHT JOIN t2 ON t1.f1=t2.f2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Get all records from two tables and line up matches
SELECT t1.f1, t2.f2 FROM t1 FULL JOIN t2 ON t1.f1=t2.f2
26
Use two SELECTs in one statement to get distinct values
SELECT f1 FROM t1 UNION SELECT f2 FROM t2;
27
Use two SELECTs in one statement to get all values
SELECT f1 FROM t1 UNION ALL SELECT f2 FROM t2;
28
Copy data to another table
SELECT * INTO newtable [IN db] FROM oldtable
29
Make a new database
CREATE DATABASE name
30
Create a new table
CREATE TABLE name (fieldname type constraint, fieldname type constraint)
31
Constraint so that field will not accept null values
NOT NULL
32
Constraint so that field will will have unique values
UNIQUE
33
Add a constraint to existing field
ALTER TABLE table ADD constraint (fieldname)
34
Drop a constraint from existing field
ALTER TABLE table DROP CONSTRAINT field
35
Constraint for primary key
PRIMARY KEY
36
Constraint for key that maps to another table
FOREIGN KEY
37
Add foreign key constraint to existing field
ALTER TABLE tablename ADD FOREIGN KEY fieldname REFERENCES othertable(otherfield)
38
Constraint on a field to restrict values of that field (e.g. int above 0)
CHECK (field>0)
39
Constraint for multiple columns in a table (when creating table)
CREATE TABLE name (..., CONSTRAINT checkname CHECK (field>0 AND field='Test');
40
Add constraint for single column in a table (existing table)
ALTER TABLE tablename ADD CHECK (fieldname>0)
41
Add constraint for multiple columns in a table (existing table)
ALTER TABLE tablename ADD CONSTRAINT checkname CHECK (field>0 AND field='test');
42
Drop a check constraint
ALTER TABLE table DROP CONSTRAINT checkname
43
Default value constraint
DEFAULT defaultvalue
44
Add default value constraint
ALTER TABLE tablename ALTER COLUMN fieldname SET DEFAULT defaultvalue
45
Drop default value constraint
ALTER TABLE tablename ALTER COLUMN fieldname DROP DEFAULT
46
Add an index to a table that allows duplicate values
CREATE INDEX indexname ON tablename (fieldname)
47
Add an index to a table that doesn't allow duplicates
CREATE UNIQUE INDEX indexname ON tablename (fieldname)
48
Remove an index
DROP INDEX tablename.indexname
49
Delete a table
DROP TABLE tablename
50
Delete a database
DROP DATABASE dbname
51
Clear all data in a table
TRUNCATE TABLE tablename
52
Add a field to a table
ALTER TABLE tablename ADD fieldname datatype
53
Delete a field from a table
ALTER TABLE tablename DROP COLUMN fieldname
54
Change a field's data type
ALTER TABLE tablename ALTER COLUMN fieldname newtype
55
Auto-increment constraint
IDENTITY(start,delta)
56
Create a view of a query
CREATE VIEW [viewname] AS SELECT...
57
Get all records from a view
SELECT * FROM [viewname]
58
Get rid of a view
DROP VIEW [viewname]
59
Function for current date and time
GETDATE()
60
Function for single part of a date/time
DATEPART(whichpart,date)
61
Function to add/subtract time from date
DATEADD(whichpart,number,date)
62
Function to get time interval between two dates
DATEDIFF(whichpart,startdate,enddate)
63
Function to change formats of types (e.g. date to string)
CONVERT(VARCHAR(19),GETDATE(),10) datetype,expression,style
64
Get records where a field is null
SELECT field FROM tablename WHERE field IS NULL
65
Get records where a field is not null
SELECT field FROM tablename WHERE field IS NOT NULL
66
Function to safely handle when field is null
ISNULL(field,valuetoreturnifnull)
67
Data type - fixed length character string
char(n)
68
Data type - variable length char string 8k characters
varchar(n)
69
Data type - variable length char string 1bn characters
varchar(max)
70
Data type - variable length char string 2GB data
text
71
Data type - fixed length unicode character string 4k characters
nchar(n)
72
Data type - variable length unicode character string 4k characters
nvarchar(n)
73
Data type - variable length unicode character string 500k characters
nvarchar(max)
74
Data type - variable length unicode char string 2GB data
ntext
75
Data type - boolean 0/1/null
bit
76
Data type - fixed-length binary data, 8kb
binary(n)
77
Data type - variable length binary data, 8kb
varbinary(n)
78
Data type - variable length binary data, 2gb
varbinary(max) or image
79
Data type - 1 byte integer, unsigned
tinyint
80
Data type - signed 2 byte integer
smallint
81
Data type - signed 4 byte integer
int
82
Data type - signed 8 byte integer
bigint
83
Data type - fixed-point number (5-17 bytes)
decimal(totaldigits, fractionaldigits) or numeric(t, f)
84
Data type - 4 byte monetary data
smallmoney
85
Data type - 8 byte monetary data
money
86
Data type - floating point number (4 or 8 bytes)
float(24) or float(53)
87
Data type - floating point number (4 bytes)
real
88
Data type - 8 byte date and time
datetime or datetime2 or datetimeoffset
89
Data type - 4 byte date and time
smalldatetime
90
Data type - 3 byte date only
date
91
Data type - 3-5 byte time only
time
92
SQL Aggregate functions (calculated from multiple values) 5x
AVG, COUNT, MAX, MIN, SUM
93
SQL Scalar functions (calculated from single input) 7x
UPPER, LOWER, MID, LEN, ROUND, NOW, FORMAT
94
Like a pivot table - grouping aggregate function for sum
SELECT field, func(field) FROM table WHERE field>0 GROUP BY field
95
Get records with values conditionally tested against aggregate function
SELECT field FROM table GROUP BY field HAVING SUM(field)<2000