sql-select Flashcards

1
Q

What is SQL and how is it different from languages like JavaScript?

A

SQL is the primary way of interacting with relational databases. It is different from JavaScript because JavaScript is an imperative language where the programmer tells the JavaScript runtime what to do and how to do it (storing values in variables, making choices with conditionals, repeating a step a number of times with loops). SQL is a declarative language where programmers describe the results they want and the programming environment comes up with its own plan for getting those results. Relational databases interpret SQL and then dynamically generates a plan of action to perform the programmer’s commands as efficiently as possible.

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

How do you retrieve specific columns from a database table?

A

You query the database for the specific columns with the “select” keyword followed by the column names you want surrounded by double quotes (“ “) and separated by commas, followed by the “from” keyword specifying the table you want to retrieve the data from. A semicolon is placed at the end of this query.

select (name of column in double quotes),
(name of column in double quotes)
from (name of first table in double quotes);

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

How do you filter rows based on some specific criteria?

A

You query the database by typing the “select” keyword, followed by the columns (or attributes) you want surrounded by double quotes (“ “) and separated by commas, followed by the “from” keyword, followed by the name of the table surround by double quotes, then followed by the “where” keyword, followed by name of the category in double quotes, followed by an equal sign (=), followed by the name of the column (or attribute) surrounded in single quotes (‘ ‘), and followed by a semicolon.

select (name of column in double quotes),
(name of column in double quotes)
from (name of table in double quotes)
where (name of category in double quotes) = (name of column in single quotes);

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

What are the benefits of formatting your SQL?

A

It makes it easier to read and provides consistent formatting.

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

What are four comparison operators that can be used in a where clause?

A

The equal sign (=), greater than (>), less than (<), and note equal to (!=)

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

How do you limit the number of rows returned in a result set?

A

With the “limit” keyword followed by a literal integer number at the end of the select statement.

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

How do you retrieve all columns from a database table?

A

select *

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

How do you control the sort order of a result set?

A

Using the “order by” clause followed by the name of the column you want to order the results by surrounded in double quotes (“ “). Additionally, you can order them in descending order by typing ‘desc’. Default sort order is ascending.

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