SQL Basics Flashcards

1
Q

Creating Tables

A

CREATE TABLE Students(name VARCHAR(20), identification INT)

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

Constraints

A

CONSTRAINT PRIMARY KEY (identification)

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

Dropping Tables

A

DROP TABLE Students;

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

Inserting into tables

A

INSERT INTO Students(name, identification) VALUES (‘Jake’, 204902);

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

Selecting Values

A

SELECT * FROM Students;

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

Modifying Tables

A

ALTER TABLE Students ADD email VARCHAR(100);
ALTER TABLE Students MODIFY email VARCHAR(200);

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

Deleting Values

A

DELETE FROM Students WHERE name=’Jake’

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

Updating Values

A

UPDATE Students SET identification=20139 WHERE name=’Jake’

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

Conditions

A
  • NOT -> use everything except what is specified
  • BETWEEN -> between integers (10 and 20 etc)
  • LIKE -> for string matching
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Projection

A

Allows you to choose what to select (SELECT * ,SELECT Events etc. You could have SELECT Grades, Name, aka multiple).

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

Distinct

A

Removes duplicate rows (SELECT DISTINCT.

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

Renaming

A

Allows you to rename attributes (SELECT Event AS Activity, SELECT first_name, family_name AS Surname)

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

New Columns

A

Using math and AS to create new columns (SELECT Price * Number AS Total_cost).

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

Cross Product

A

In a way, its joining tables (SELECT first_name, grade FROM Students, Results).

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

Natural Join

A

Another way of doing it (SELECT * FROM Employees NATURAL JOIN Transactions)

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

Removing Duplicates

A

DISTINCT

17
Q

WHERE Statements (WHERE IN, WHERE EXISTS)

A

SELECT * FROM Students WHERE name IN(‘John’, ‘Sam’). You could also use IN(SELECT name FROM Lecturers) instead of having to provide all the names.

SELECT * FROM Students WHERE EXISTS (SELECT 1 FROM Lecturers WHERE Students.name=Lecturers.name AND Students.last_name=Lecturers.last_name). A generalisation of IN and is used to find multiple.

18
Q

Grouping

A
  • Shows us how many things are connected to what is specified (SELECT id, count FROM Transactions GROUP BY id - this would show the amount of connections id has and store them in count).
19
Q

Ordering

A

ORDER BY.

20
Q

Views

A
  • CREATE OR REPLACE VIEW - this would either create the virtual table or simply rewrite it with whatever is specified.
  • DROP VIEW… -> drops the table
21
Q

Combining Two Tables Sequentially

A

UNION:
Combines two tables together into one output.
select (contactFirstName + ‘ ‘ + contactLastName) as contactName FROM customers UNION
select customerName FROM customers