SQL Select Flashcards

1
Q

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

A

SQL (Structured Query Language) is a declarative programming language for querying databases. SQL is the primary way of interacting with relational databases. It is a powerful way of retrieving, creating, and manipulating data in a relational database.

SQL is a declarative programming language. In declarative languages, programmers describe the results they want and the programming environment comes up with its own plan for getting those results.

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

Using the select statement, followed by a comma-separated list of column names, each surrounded by double quotes.

select “name”,
“price”
from “products”;

  • The column names are followed by a from clause specifying which table to retrieve the data from.
  • The query must end in a ; semicolon.
    SQL keywords such as select and from are not case-sensitive.
  • SQL does not have to be indented, but you should do it anyway for consistent style and therefore readability.
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

This is done using a where clause.

select "productId",
       "name",
       "price"
  from "products"
 where "category" = 'cleaning';
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

Consistent style and therefore readability

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

Greater than “>”, less than “

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

By using the “limit” clause followed by the number limit

select "name",
       "description"
  from "products"
 order by "price" desc
 limit 1;
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

With select and the asterisk

select *
from “products”;

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

By using the “order by” clause

select *
from “products”
order by “price”;

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