SQL Flashcards

SQL

1
Q

What’s the syntax to refer to both a table name and column name in a SELECT statement?

A

tableName.columnName

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

In SQLite, how do you format the output of a select statement?

A

output the name of each column

.headers on

now we are in column mode, enabling us to run the next two .width commands

.mode column

adjusts and normalizes column width

.width auto

customize column device-width

.width NUM1, NUM2

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

In SQLite, what is the syntax for selecting a row where a column value is null?

A

SELECT * FROM cats WHERE name IS NULL;

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

In SQLite, what is the syntax for inserting a row that includes null values?

A

INSERT INTO cats (name, age, breed) VALUES (NULL, NULL, “Tabby”);

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

What is the syntax to retrieve two columns from two different tables when the column names are identical?

A

SELECT cats.name, dogs.name FROM cats, dogs;

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

In SQLite, what is the syntax for inserting a row into a table?

A

INSERT INTO cats (name, age, breed) VALUES (‘Maru’, 3, ‘Scottish Fold’);

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

In SQLite, do you have to add the primary key when inserting a row into a table?

A

No. Primary Key columns are auto-incrementing. As long as you have defined an id column with a data type of INTEGER PRIMARY KEY, a newly inserted row’s id column will be automatically given the correct value.

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

In SQLite, what is the syntax for an UPDATE statement?

A

sqlite> UPDATE cats SET name = “Hana” WHERE name = “Hannah”;

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

In SQLite, what is the syntax to delete a row from a table?

A

sqlite> DELETE FROM cats WHERE id = 2;

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

What does it mean to “type” a table?

A

“Typing refers to adding data types to row definitions when creating a table.”

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

In SQLite, what is the TEXT datatype?

A

Any alphanumeric characters which we want to represent as plain text.

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

In SQLite, what is the BLOB datatype?

A

generally used for holding binary data

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

In SQLite, what are the datatypes?

A

TEXT, INTEGER, REAL, BLOB

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

In SQLite, what is the INTEGER datatype? And when do you use it?\n

A

Anything we want to represent as a whole number. If it’s a number and contains no letter or special characters or decimal points then we should store it as an integer. If we use it to perform math or create a comparison between two different rows in our database, then we definitely want to store it as an integer. If it’s just a number, it’s generally not a bad idea to store it as an integer. You might never add two house address numbers together, but you might want to sort them numerically. For example in the preceding case, you might want to get the biggest number and not the longest piece of text.

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

In SQLite, how many actual datatypes are there?

A

SQLite allows the programmer to use other common datatypes outside of the four mentioned above. This is why we are referring to TEXT INTEGER REAL BLOB as datatype “categories”. All other common datatypes are lumped into one of the four existing datatypes recognized by SQLite.

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

In SQLite, what is the REAL datatype?

A

Anything that’s a plain old decimal like 1.3 or 2.25. SQLite will store decimals up to 15 characters long. You can store 1.2345678912345 or 1234.5678912345, but 1.23456789123456789 would only store 1.2345678912345. In other database systems this is called ‘double precision.’

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

What is the Double Precision datatype?

A

A decimal number with up to 15 digits.

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

What is the syntax to execute a file in the CLI?

A

sqlite3 mydb.db < my_sql_file.sql

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

In SQLite, what command will exit out of the sqlite CLI?

A

.quit

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

In SQLite, what is the command to enter a database?

A

sqlite3 pets_database.db

21
Q

How do you create a table in SQLite?

A

\n sqlite> CREATE TABLE cats (\n id INTEGER PRIMARY KEY,\n name TEXT, \n age INTEGER\n );\n

22
Q

What command in SQLite will show a table’s structure?\n

A

\n sqlite> .schema\n CREATE TABLE cats(\n id INTEGER PRIMARY KEY,\n name TEXT,\n age INTEGER\n );

23
Q

How are words in a column name separated?

A

when we have multiple words in a column name, we link them together using underscores rather than spaces. We call this convention “snake_case”.

24
Q

Should column names be upper case or lower case?

A

By convention, always use lowercase letters when referring to columns.

25
Q

In SQLite, what is the syntax to add a column to a table?

A

sqlite> ALTER TABLE cats ADD COLUMN breed TEXT;

26
Q

In SQLite, what command will remove a table?

A

sqlite> DROP TABLE cats;

27
Q

Which command in SQLite will list all tables in the database?

A

Use the .tables command to list all the tables in a database

28
Q

Which command in SQLite will show the structure of the database?

A

Use the .schema command to look at the structure of a database

29
Q

How do you create a new database from the command line in SQLite?

A

sqlite3 test_sqlite.db

30
Q

What does the sqlite cli look like when it starts? What does the prompt look like?

A

\n SQLite version 3.7.12 2013-03-19 12:42:02\n Enter “.help” for instructions\n Enter SQL statements terminated with a “;”\n sqlite>\n

31
Q

How do you create a table in SQLite?

A

sqlite> create table test_table(id);

32
Q

What happens if you don’t terminate a SQLite command with a semicolon?

A

All SQL statements that you write in your terminal, inside the sqlite prompt, sqlite3>, must be terminated with a semi-colon ;. If you hit enter without adding a semi-colon to the end of your line, you will be trapped! Don’t worry though, just add that ; on the new line and hit enter again. The only command that doesn’t require, and in fact doesn’t even work with, a ; is the .quit command.

33
Q

What is the difference between SQL and NoSQL?

A

SQL (Structured Query Language) is a language for managing data in a database. Unlike some other programming languages, it’s only used for one thing: talking to databases. Thus, you might hear it referred to as a “special purpose”, or “domain specific” programming language. NoSQL databases are structured differently from the relational database structure. With relational, we structure tables by the type of relations, but NoSQL keeps all the information in one place, in the form of key-values or documents. For example, consider a database of people and their subscriptions to a newsletter. Using a relational database structure, we might separate the information for each person, having their id, name, and email, in one table named customers, and then another table for the subscriptions, having the newsletter name, and other information associated with the subscriptions. With a NoSQL database, instead of separating information in this way, we might just have a single document with the person’s information, as well as the subscription information, all in one document. As you might tell, this type of structuring might have some benefits and some cons compared to relational databases.

34
Q

What is an outer join?

A

An outer join will combine rows from different tables even if the join condition is not met. In a LEFT JOIN, every row in the left table is returned in the result set, and if the join condition is not met, then NULL values are used to fill in the columns from the right table.

35
Q

What is the syntax for a LEFT JOIN?

A

\n SELECT column_name\n FROM table1\n LEFT JOIN table2\n ON table1.column_name = table2.column_name;\n

36
Q

What does the WITH clause do?

A

>The WITH clause stores the result of a query in a temporary table using an alias. Multiple temporary tables can be defined with one instance of the WITH keyword.

37
Q

What is the syntax for a WITH clause?

A

\n WITH temp_table \n AS (SELECT * FROM actual_table)\n SELECT *\n FROM temp_table\n WHERE year BETWEEN 2000 AND 2020;\n

38
Q

What does the UNION clause do?

A

The UNION clause is used to combine results that appear from multiple SELECT statements and filter duplicates.

39
Q

What is the syntax for the UNION clause?

A

\n SELECT name\n FROM first_names\n UNION\n SELECT name\n FROM last_names\n

40
Q

What does the CROSS JOIN clause do?

A

The CROSS JOIN clause is used to combine each row from one table with each row from another in the result set. This JOIN is helpful for creating all possible combinations for the records (rows) in two tables.

41
Q

What is the syntax for the CROSS JOIN clause?

A

\n SELECT shirts.shirt_color,\n pants.pants_color\n FROM shirts\n CROSS JOIN pants;\n

42
Q

What does an INNER JOIN do?

A

The JOIN clause allows for the return of results from more than one table by joining them together with other results based on common column values specified using an ON clause. INNER JOIN is the default JOIN and it will only return results matching the condition specified by ON.

43
Q

What is the default join type?

A

INNER JOIN

44
Q

What is the syntax for an INNER JOIN?

A

\n SELECT * \n FROM books\n JOIN authors\n ON books.author_id = authors.id;\n

45
Q

What does GROUP BY do?

A

GROUP BY

46
Q

What does ORDER BY do?

A

ORDER BY

47
Q

What does the HAVING clause do?

A

The HAVING clause is used to further filter the result set groups provided by the GROUP BY clause. HAVING is often used with aggregate functions to filter the result set groups based on an aggregate property.

48
Q

What is the syntax for the HAVING clause?

A

\n SELECT year, \n COUNT(*) \n FROM movies \n GROUP BY year\n HAVING COUNT(*) > 5;