SQL / PostgreSQL Flashcards
What is SQL and how is it different from languages like JavaScript?
SQL is a querying language, unlike JavaScript it’s declarative, the user declares the desired end-result and doesn’t actually program how it’s achieved.
How do you retrieve specific columns from a database table?
select “columnNameOne”, “columnNameTwo” from “table”
How do you filter rows based on some specific criteria?
where “attribute” = ‘value’
What are the benefits of formatting your SQL?
Easier readability for others and your future self.
What are four comparison operators that can be used in a where clause?
=, !=, greater than, less than
How do you limit the number of rows returned in a result set?
limit number
How do you retrieve all columns from a database table?
select * from “table”
How do you control the sort order of a result set?
order by “attributeName”
How do you add a row to a SQL table?
insert into “table” (“attrOne”, “attrTwo”, … “attrN”)
values (‘valOne’, ‘valTwo’, … ‘valN’)
What is a tuple?
A row of data in a table.
How do you add multiple rows to a SQL table at once?
Add more tuples after values and separate them with commas.
How do you get back the row being inserted into a table without a separate select statement?
returning *
How do you update rows in a database table?
update “table”
set (“colOne” = ‘valOne’, “colTwo” = ‘valTwo’, … “colN” = ‘valN’)
where condition;
Why is it important to include a where clause in your update statements?
So updates are only applied to the desired rows.
How do you delete rows from a database table?
delete from “table”
where condition;
How do you accidentally delete all rows from a table?
You forget the where statement.
What is a foreign key?
A key that refers a primary key on another table.
How do you join two SQL tables?
select * from “tableOne”
join “tableTwo” using (“sharedKey”)
How do you temporarily rename columns or tables in a SQL statement?
select “name” as “tempName” from “table” as “tempTable”
What are some examples of aggregate functions?
count(), min(), max(), sum(), avg()
What is the purpose of a group by clause?
The group by clause groups rows that share common attributes in separate groups before performing aggregate functions on each of them.
What is PostgreSQL and what are some alternative relational databases?
PostgreSQL is a relational database management system. Other RDBMS include MySQL, SQL Server by Microsoft, and Oracle DB.
What are some advantages of learning a relational database?
They are widely used in tech.
What is one way to see if PostgreSQL is running?
Run “sudo service postgresql status” in the command line.
What is a database schema?
A schema is a collection of tables.
What is a table?
A list of rows that share the same set of attributes.
What is a row?
A single structured entry in the table.