SQL Select Flashcards
What is SQL and how is it different from languages like JavaScript?
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 do you retrieve specific columns from a database table?
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 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?
Consistent style and therefore readability
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?
By using the “limit” clause followed by the number limit
select "name", "description" from "products" order by "price" desc limit 1;
How do you retrieve all columns from a database table?
With select and the asterisk
select *
from “products”;
How do you control the sort order of a result set?
By using the “order by” clause
select *
from “products”
order by “price”;