Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 23 Next »

5. Getting Started with SQL

Structured Query Language (SQL) is a standardized programming language that is used to manage relational databases and perform various operations on the data in them. There are many different dialects of SQL, and whilst they are broadly similar, the more advanced dialects like that used in PostgreSQL offers additional functions.  SQL in PostgreSQL conforms to the ANSI (American National Standards Institute) standard. To be compliant with ANSI, systems must support the major commands, including SELECT, UPDATE, DELETE, INSERT, WHERE, so many statements will be similar to that used in other systems like Oracle and SQL Server.

5.1. SQL in pgAdmin

pgAdmin has a query window to use for interaction with the database with user-defined SQL. You can access it through the Tools menu or the right-click menu.

The query window is divided into three areas:

  • an input pane for writing queries

  • an output pane to receive results

  • a scratch pad for making notes

The query windows allows the user to:

  • execute a query and see the results in the results pane

  • execute a query and save results to a text file

  • save a query as SQL

  • open a saved query

pgAdmin includes some standard scripts and functions that are available by right-clicking on table name. These allow you to view all, or a subset of the data in a table, or construct common SQL scripts

5.2. Querying data

A simple query might return a sub-set of the data held within the database. In English there are multiple ways of posing a question, whereas in SQL you need to be specific about the language you use and the precise columns, tables, schemas and databases you are referring to. Fortunately, pgAdmin provides some tools to help construct these queries.

So if you wanted to generate a list of names of all the Parishes and their areas in hectares within the county of Surrey, the SQL query needed might look like this:

SELECT name, hectares
FROM boundaryline.parish
WHERE file_name = 'SURREY_COUNTY';

Note keywords or statements like SELECT, FROM and WHERE are not case case sensitive (SELECT = select = SeLecT), but by convention we use uppercase for all SQL keywords in order to make queries easier to read. Queries can be composed as a single line, or spread over a number of lines to ease reading. A semicolon ( ; ) is used to signal to PostgreSQL the end of an SQL statement

Single Quotes contain string values e.g. name = ‘Epsom’. Only use these with string data types such as text and character varying.

String values are case sensitive so SELECT * FROM general settlement WHERE name like 'Epsom'; and SELECT * FROM general settlement WHERE name like 'ePsOm'; will produce different results.

If the string itself includes a quote, replace the single quotes with $$ e.g. name = $$St. David’s$$

Double quote enclose database object names that:

  • include a space

  • use upper case characters

  • use ‘special’ characters like punctuation

  • are a reserved SQL word

Familiarity with pgAdmin

  1. Open pgAdmin and explore the hierarchy of tables within schemas of your training database

  2. Identify the school table within the general schema

  3. Click on the SQL tab and note SQL commands necessary to create the table

  4. Right-click > school > View/Edit Data > First 100 Rows to display all the data for the first 100 records within the school table

  5. Note the query to generate these result is displayed in the Query pane, and the Data Output is displayed within the results pane

5.3. SELECT

The most basic form of the SELECT statement retrieves data from a single table

SELECT [select_list]
FROM [table_name];
  • [select_list] can be a single column, or a list of columns from the table you want to retrieve data from. A comma ( , ) is used to separate the individual columns. If you want to return all columns of the table you can use an asterisk ( * ) in place of [select_list]. The [select_list] may also contain an expressions or literal values.

  • The FROM clause is optional and specifies the name of the table from which you want to query data.

The SELECT statement can be used to form incredibly flexible queries by including additional clauses

  • Select distinct rows using DISTINCT operator.

  • Sort rows usingORDER BY clause.

  • Filter rows using WHERE clause.

  • Select a subset of rows from a table using LIMIT or FETCH clause.

  • Group rows into groups using GROUP BY clause.

  • Filter groups using HAVING clause.

  • Join with other tables using joins such as INNER JOIN, LEFT JOIN, FULL OUTER JOIN, CROSS JOIN clauses.

  • Perform set operations using UNION, INTERSECT, and EXCEPT.

Exercise - use of SELECT

  1. Open the Query Tool

  2. Compose a query in the Query pane and execute (F5 or play button)

  3. Try different variation of column names, noting the order of the the column names in the query impacts the order in the results

5.4. SELECT DISTINCT

The DISTINCT clause is used in a SELECT statement to remove duplicate values. It can be applied to one or more columns in the [select_list]. The DISTINCT clause is often used with the COUNT() function in order to determine the quantity of records of a particular type

SELECT 
  DISTINCT [column_name]
FROM 
  [table_name];
  • [select_list] can be a single column, or a list of columns from the table

  • COUNT() function can be used to determine number of records

  • The keyword AS can be used to set a column alias which is more meaningful to the reader

Exercise - use of SELECT DISTINCT

  1. In the Query Tool compose a query to produce a list of wards that contain the highest number of schools together with that number

  2. The the COUNT() function to determine how many records meet this condition

  3. Use the AS keyword to give the column a sensible name, note you made need to put this in double quotes if you are including spaces of special characters

5.5. ORDER BY

The ORDER BY clause allows you to sort rows returned by a SELECT clause in ascending or descending order using ASC or DESC options. It is possible to sort based on multiple columns by separating expressions with a comma ( , ).

The syntax employed follows this format;

SELECT 
  [select_list]
FROM 
  [table_name]
ORDER BY
  [sort_expression1] [ASC | DESC]
  ...
  [sort_expression2] [ASC | DESC];

Exercise - ORDER BY

  1. In your query window amend your previous query to add an ORDER BY clause

  2. Try sorting in ascending and descending order

  3. SELECT parliamentary_constituency, name, establishment_number, school_address
    	FROM general.school
    	ORDER BY  parliamentary_constituency, name DESC;
  4. Note you can order by more than one column

5.6. WHERE

The WHERE clause can be used to return records that satisfy a specified condition. The condition must evaluate as true or false with only records where condition is true to be included within the result set. The following comparison and logical operators may be used to form the condition.

Operator

Description

=

Equal

>

Greater than

<

Less than

>=

Greater than or equal

<=

Less than or equal

<> or !=

Not equal

AND

Logical operator AND

OR

Logical operator OR

IN

Return true if a value matches any value in a list

BETWEEN

Return true if a value is between a range of values

LIKE

Return true if a value matches a pattern

IS NULL

Return true if a value is NULL

NOT

Negate the result of other operators

Exercise - WHERE

  1. In your query window amend your previous query to add a WHERE clause

  2. Which school has an establishment_number of 7014 (WHERE establishment_number='7014')

  3. List all the primary schools in the parliamentary constituency of Guildford

  4. SELECT parliamentary_constituency, name, establishment_number, school_address, phase_of_education
    	FROM general.school
    	WHERE parliamentary_constituency LIKE 'Guildford' AND phase_of_education ='Primary'
    	ORDER BY  parliamentary_constituency, name DESC;
  5. Select all schools that a six year old might attend

  6.  

5.7. LIMIT

LIMIT is an option clause of the SELECT statement that constrains the number of rows returned by the query. This is useful as it provides a sample of the data and with large data sets generally executes quicker than without the clause.

You can optionally use the ORDER BY clause to return the first or last records in a series e.g. the largest or smallest values.

Optionally you can also use the OFFSET clause to start from the nth record.

SELECT 
  [select_list]
FROM 
  [table_name]
ORDER BY
  [sort_expression1]
LIMIT [row_count]
OFFSET [number_of_records];

5.8. SQL in QGIS

pgAdmin has a query window to use for interaction with the database with user-defined SQL. You can access it through the Tools menu or the right click menu.

Name

Description

character varying(n), varchar(n)

variable-length with limit

character(n), char(n)

fixed-length, blank padded

text

variable unlimited length

  • No labels