SQL Flashcards
What is PostgreSQL and what are some alternative relational databases?
PostgreSQL is a powerful, free, open source Relational Database Management System (RDBMS). Other popular relational databases include MySQL (also free), SQL Server by Microsoft, and Oracle by Oracle Corporation.
What are some advantages of learning a relational database?
A quality of many relational databases is that they support good guarantees about data integrity. They can store and modify data in a way that makes data corruption as unlikely as possible. This means that developers can set up their database to reject “bad” data and not worry about data being “half written”.
They are very widely used
What is one way to see if PostgreSQL is running?
Using the top command in one of your terminals to list the currently running processes
You can also check the status of the postgresql service:
sudo service postgresql status
What is a database schema?
A collection of tables is called a schema. A schema defines how the data in a relational database should be organized. In relational databases, you typically have to define your schema up front and the database server will make sure that any data being written to the database conforms to that schema.
What is a table?
A table is a list of rows each having the same set of attributes. For example, all students in a “students” table could have “firstName”, “lastName”, and “dateOfBirth” attributes. Attributes are commonly referred to as columns. You might visualize a database table as a sort of spreadsheet where each row is a record in that spreadsheet.
What is a row?
The row is where actual data is stored
What is SQL and how is it different from languages like JavaScript?
Structured Query Language) 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). It is particularly useful in handling structured data, i.e. data incorporating relations among entities and variables.
JavaScript is an imperative programming language, where you basically tell the JS runtime what to do and how to do it. Whereas, SQL is a declarative programming language. Programmers describe the results they want and the programming environment comes up with its own plan for getting those results (like HTML and CSS). The programmer simply “declares” their intent and the Web browser does its best to output the desired results according to a predefined set of rules.
How do you retrieve specific columns from a database table?
Querying the data
- 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.
- 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.
i.e.
select “name”,
“price”
from “products”;
It is possible to select all of the columns in a table by replacing the list of column names with an * asterisk.
select *
from “products”;
- The order of the results is not predictable.
- The * asterisk is not in quotes.
How do you filter rows based on some specific criteria?
This is done using a where clause.
select “productId”,
“name”,
“price”
from “products”
where “category” = ‘cleaning’;
What are the benefits of formatting your SQL?
Database tables in large systems can contain millions of rows. As humans we cannot really understand that much information all at once. Plus, the types of problems that we are solving usually involve a few pieces of data at a time. So formatting allows us to interpret the data more efficiently. Easier to read, understand, and debug it.
What are four comparison operators that can be used in a where clause?
=, <, >, !=
How do you limit the number of rows returned in a result set?
SQL provides us with the ability to limit the number of rows included in a result set.
select “name”,
“description”
from “products”
order by “price” desc
limit 1;
Before we look at the result set, here are some things to note about the example select statement:
The limit clause comes last.
The limit clause includes a literal integer number with no quotes to specify the maximum number of rows that should be returned.
The sort order of the order by clause is switched to descending order with the desc keyword.
How do you retrieve all columns from a database table?
It is possible to select all of the columns in a table by replacing the list of column names with an * asterisk.
select *
from “products”;
How do you control the sort order of a result set?
The default sort order of the results is ascending order, but you can change it to descending
How do you add a row to a SQL table?
An SQL insert statement is a means of adding rows to a table. Consider the following “products” table:
To add a row to this table, we would likely execute the following command:
insert into “products” (“name”, “description”, “price”, “category”)
values (‘Ostrich Pillow’, ‘Feel comfy and cozy!’, 99, ‘self care’);
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.