SQL Flashcards

1
Q

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

A

Structured Query Language(SQL) is a domain-specific language used in programming and designed for managing data held in a relational database management system (RDBMS), or for stream processing in a relational data stream management system (RDSMS).

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
  • The query starts with the select keyword.
  • The select keyword is followed by a comma-separated list of column names, each surrounded by “ double quotes.
  • The column names are followed by a from clause specifying which table to retrieve the data from.
  • The query must end in a ; semicolon.

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

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
Use the WHERE statement when filtering for specific values or the SELECT statement when filtering for specific attributes
ex. 
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
  • Helps us to make the queries more readable and easy to understand.
  • When the queries are formatted and laid out with proper spacing it comes easy to find the errors in the query and fix the error.
  • Formatted queries are easy to be passed on to the fellow developers.
  • Helps in increasing the efficiency of the database transaction system.
  • Makes it easy to scale and deploy the database system.
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

=, <, >, !=

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
  • Use the limit clause.
  • The limit clause always comes last.
  • The limit clause includes a literal integer number with no quotes to specify the maximum number of rows that should be returned.
    ex.
    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

Use the * asterisk character instead of column names.
Good for seeing what type of data is inside the table.
ex.
select *
from “”;

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

Use the “order by” clause in the select statement to control the order of the result set.
- The order by clause comes after the from clause.
- The order by clause is followed by a column name in “ double quotes.
- The default sort order of the results is ascending order.
- The sort order of the order by clause is switched to descending order with the desc keyword.
ex.
select *
from “products”
order by “price”;
OR
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
9
Q

How do you add a row to a SQL table?

A
  • The statement begins with the insert keyword.
  • The table to insert into is specified in “ double quotes.
  • The list of columns being inserted is wrapped in () parenthesis.
  • The values being inserted are also wrapped in () in parenthesis in the same order as the columns they belong to. In SQL, a list of values is referred to as a tuple.
  • Text values are wrapped in ‘ single quotes.
  • Numeric values are represented with literal numbers (or decimals if applicable).
  • In this particular statement, the “productId” was left out. This is because tables are often configured to auto-generate identifier attributes to avoid accidental duplicates.
    Ex.
    insert into “products” (“name”, “description”, “price”, “category”)
    values (‘Ostrich Pillow’, ‘Feel comfy and cozy!’, 99, ‘self care’);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a tuple?

A

ex.
insert into “products” (“name”, “description”, “price”, “category”)
values (‘Ostrich Pillow’, ‘Feel comfy and cozy!’, 99, ‘self care’);
- The values being inserted are also wrapped in () in parenthesis in the same order as the columns they belong to. In SQL, a list of values is referred to as a tuple.

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

How do you add multiple rows to a SQL table at once?

A

ex.
insert into “products” (“name”, “description”, “price”, “category”)
values (‘Ostrich Pillow’, ‘Feel comfy and cozy!’, 99, ‘self care’),
(‘Tater Mitts’, ‘Scrub some taters!’, 6, ‘cooking’)
returning *;
- Data rows can be batch inserted into a database table by specifying more than one tuple of values, separated by commas. Below we are inserting two new rows into the “products” table.

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

How do you get back the row being inserted into a table without a separate select statement?

A
  • In PostgreSQL it’s possible to get the full inserted row back from the database, including it’s auto-generated attribute(s), like a productId. This is done with a returning clause.
    ex.
    insert into “products” (“name”, “description”, “price”, “category”)
    values (‘Ostrich Pillow’, ‘Feel comfy and cozy!’, 99, ‘self care’)
    returning *;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you update rows in a database table?

A
  • An SQL update statement is a means of updating rows in a database table.
  • Great care must be taken to include a where clause in your update statements to only target specific rows
    ex.
    update “products”
    set
    “price” = 100
    “colName” = ‘value’ <=== (can update multiple columns)
    where “productId” = 24;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why is it important to include a where clause in your update statements?

A

A where clause is necessary in order to target only specific rows.
If it is excluded, it will update everything.

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

How do you delete rows from a database table?

A

An SQL delete statement is how rows get removed from tables.

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

How do you accidentally delete all rows from a table?

A

You can accidentally delete all rows by excluding a where clause.

17
Q

What is a foreign key?

A

It is a column in a table that has values constrained to match a column in another table.

18
Q

How do you join two SQL tables?

A
Use the join clause.
ex. 
select *
  from "products"
  join "suppliers" using ("supplierId");
19
Q

How do you temporarily rename columns or tables in a SQL statement?

A
You use the "as" keyword.
ex. 
select "products"."name" as "product",
       "suppliers"."name" as "supplier"
  from "products"
  join "suppliers" using ("supplierId");
20
Q

What are some examples of aggregate functions?

A

max(), min(), avg(), sum()

21
Q

What is the purpose of a group by clause?

A

The GROUP BY Clause is utilized in SQL with the SELECT statement to organize similar data into groups. It combines the multiple records in single or more columns using some functions.

22
Q

What do aggregate functions do?

A

They aggregate a list or row into one value