Filtering and Concatenating in SQL


More basic notes on how to work in SQL, that I'm picking up as I go through Getting Started with SQL, an O'Reilly guide by Thomas Nield.

You can combine or concatenate fields using a double pipe || . You just enter field names in a table separated by the double pipes and use 'AS' to specify a new field to enter them in.

WHERE is used to filter data in SQL, as shown in this simple example.

This SQL query:

SELECT * FROM STATION_DATA

WHERE REPORT_CODE LIKE 'C%'

. . . will search the REPORT_CODE field for any entries which begin with the letter 'C'.

You can use <> or ! to filter for all entries except the specified string, or BETWEEN to search for values between two specified numbers.

SELECT * FROM STATION_DATA

WHERE YEAR BETWEEN 1980 AND 2000

Using IN before a set of values separated by commas in parentheses lets you search a specified group of values.

SELECT * FROM STATION_DATA

WHERE MONTH IN (10,11,12)

Parentheses can also be used to group Boolean queries in SQL.