Book Image

PostgreSQL Development Essentials

By : Baji Shaik
Book Image

PostgreSQL Development Essentials

By: Baji Shaik

Overview of this book

PostgreSQL is the most advanced open source database in the world. It is easy to install, configure, and maintain by following the documentation; however, it’s difficult to develop applications using programming languages and design databases accordingly. This book is what you need to get the most out of PostgreSQL You will begin with advanced SQL topics such as views, materialized views, and cursors, and learn about performing data type conversions. You will then perform trigger operations and use trigger functions in PostgreSQL. Next we walk through data modeling, normalization concepts, and the effect of transactions and locking on the database. The next half of the book covers the types of indexes, constrains, and the concepts of table partitioning, as well as the different mechanisms and approaches available to write efficient queries or code. Later, we explore PostgreSQL Extensions and Large Object Support in PostgreSQL. Finally, you will perform database operations in PostgreSQL using PHP and Java. By the end of this book, you will have mastered all the aspects of PostgreSQL development. You will be able to build efficient enterprise-grade applications with PostgreSQL by making use of these concepts
Table of Contents (17 chapters)
PostgreSQL Development Essentials
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface

Using subqueries


A subquery is a query within a query. In other words, a subquery is a SQL query nested inside a larger query. It may occur in a SELECT, FROM, or WHERE clause. In PostgreSQL, a subquery can be nested inside a SELECT, INSERT, UPDATE, DELETE, SET, or DO statement or inside another subquery. It is usually added within the WHERE clause of another SQL SELECT statement. You can use comparison operators, such as >, <, or =. Comparison operators can also be a multiple-row operator, such as IN, ANY, SOME, or ALL. It can be treated as an inner query that is an SQL query placed as a part of another query called as outer query. The inner query is executed before its parent query so that the results of the inner query can be passed to the outer query.

The following statement illustrates the subquery syntax:

SELECT column list
FROM table 
WHERE table.columnname expr_operator
(SELECT column FROM table)

The query inside the brackets is called the inner query. The query that contains the subquery is called the outer query.

PostgreSQL executes the query that contains a subquery in the following sequence:

  • First, it executes the subquery

  • Second, it gets the results and passes it to the outer query

  • Third, it executes the outer query

Let's consider an example where you want to find employee_id, first_name, last_name, and salary for employees whose salary is higher than the average salary throughout the company.

We can do this in two steps:

  1. First, find the average salary from the employee table.

  2. Then, use the answer in the second SELECT statement to find employees who have a higher salary from the result (which is the average salary).

        SELECT avg(salary) from employee;

        Result: 25000
        SELECT employee_id,first_name,last_name,salary
        FROM employee
        WHERE salary > 25000;

This does seem rather inelegant. What we really want to do is pass the result of the first query straight into the second query without needing to remember it, and type it back for a second query.

The solution is to use a subquery. We put the first query in brackets, and use it as part of

a WHERE clause to the second query, as follows:

SELECT employee_id,first_name,last_name,salary
FROM employee
WHERE salary > (Select avg(salary) from employee);

PostgreSQL runs the query in brackets first, that is, the average of salary. After getting the answer, it then runs the outer query, substituting the answer from the inner query, and tries to find the employees whose salary is higher than the average.

Note

Note: A subquery that returns exactly one column value from one row is called a scalar subquery. The SELECT query is executed and the single returned value is used in the surrounding value expression. It is an error to use a query that returns more than one row or column as a scalar subquery. If the subquery returns no rows during a particular execution, it is not an error, and the scalar result is taken to be null. The subquery can refer to variables from the surrounding query, which will act as constants during any one evaluation of the subquery.