SQL #DAY 1

POULAMI BAKSHI
3 min readJul 2, 2021

--

SQL SELECT

Basic syntax: SELECT and FROM

There are two required ingredients in any SQL query: SELECT and FROM—and they have to be in that order. SELECT indicates which columns you'd like to view, and FROM identifies the table that they live in.

Let’s start by looking at a couple columns from the housing unit table:

It will select all the column by using * :

Now,

Write a query to select all of the columns in the tutorial.us_housing_units table without using *.

Column names :

All of the columns in the tutorial.us_housing_units table are named in lower case, and use underscores instead of spaces. The table name itself also uses underscores instead of spaces. Most people avoid putting spaces in column names because it's annoying to deal with spaces in SQL—if you want to have spaces in column names, you need to always refer to those columns in double quotes.

If you’d like your results to look a bit more presentable, you can rename columns to include spaces. For example, if you want the west column to appear as West Region in the results, you would have to type:

Without the double quotes, that query would read ‘West’ and ‘Region’ as separate objects and would return an error. Note that the results will only return capital letters if you put column names in double quotes. The following query, for example, will return results with lower-case column names.

SQL WHERE

Once you know how to view some data using SELECT and FROM, the next step is filtering the data using the WHERE clause. Here's what it looks like:

How does WHERE work?

The SQL WHERE clause works in a plain-English way: the above query does the same thing as SELECT * FROM tutorial.us_housing_units, except that the results will only include rows where the month column contains the value 1.

In Excel, it’s possible to sort data in such a way that one column can be reordered without reordering any of the other columns — though that could badly scramble your data. When using SQL, entire rows of data are preserved together. If you write a WHERE clause that filters based on values in one column, you'll limit the results in all columns to rows that satisfy the condition. The idea is that each row is one data point or observation, and all the information contained in that row belongs together.

--

--

No responses yet