Numberedheadings | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||||||||||||||||||||
Getting Started with SQLStructured 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. SQL in pgAdminpgAdmin 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:
The query windows allows the user to:
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 Querying dataA 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:
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:
SELECTThe most basic form of the SELECT statement retrieves data from a single table
The SELECT statement can be used to form incredibly flexible queries by including additional clauses
Exercise - use of SELECT
SELECT DISTINCTThe 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
Exercise - use of SELECT DISTINCT
ORDER BYThe 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; Exercise - ORDER BY
WHEREThe 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.
Exercise - WHERE
LIMITLIMIT 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. SQL in QGISpgAdmin 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.
|
...