PostgreSQL

Chapter 7 - DQL (Data Query Language)

SELECT *

In PostgreSQL, the SELECT statement is one of the most basic and widely used commands in the Data Query Language (DQL). It allows you to retrieve data from one or more tables in a database. The SELECT* query is often used to fetch all columns from a table. While it's a powerful command for quickly retrieving data, beginners should be cautious when using it in large tables, as it may return more information than needed.

Here's how you can get started with SELECT* in PostgreSQL:

  1. Basic Syntax of SELECT*
    • The asterisk * is a wildcard that selects all columns in the table.
    • The simplest form is:
    SELECT * FROM table_name;
    
  2. Example: Retrieving Data from a Customers Table
    • Suppose you have a table customers in a banking system, and you want to retrieve all data about customers:
    SELECT * FROM customers;
    
    • This query returns all columns such as customer_id, name, address, and phone_number.
  3. Using SELECT* with Filters (Optional)
    • You can add a WHERE clause to filter the rows returned. For example, retrieving all customers from a specific city:
    SELECT * FROM customers WHERE city = 'New York';
    
  4. Fetching Data from Multiple Tables (JOIN)
    • When working with related tables, such as accounts and transactions, you can use JOIN to fetch data from multiple tables:
    SELECT *
    
    FROM accounts
    
    JOIN transactions ON accounts.account_id = transactions.account_id;
    
    • This will retrieve all columns from both accounts and transactions where there is a matching account_id.
  5. Common Use Cases for SELECT*
    • Testing: Useful during development to inspect the full table structure.
    • Small Tables: Ideal for retrieving all columns when tables have limited data.

    Considerations:

    • For large tables, it's better to specify only the columns you need, as SELECT* may impact performance.

By following these steps, you'll be able to use the SELECT* query effectively in PostgreSQL.

Tansy PostgreSQL Course | SELECT ALL | Chapter 7 - Video Thumbnail



TEST CODE

Fetch all columns and rows from the org_employee table.

SELECT * FROM org_employee;

Fetch all columns and rows from the org_client table.

SELECT * FROM org_client;

Fetch all columns and rows from the act_payment table.

SELECT * FROM act_payment;

Fetch specific columns and all rows from the org_client table.

SELECT client_id, first_name, last_name, gender, city
FROM org_client;

Fetch specific columns and all rows from the org_client table. The order of columns in the result set may differ from the table definition.

SELECT city, gender, first_name, last_name, email, client_id
FROM org_client;

Retrieve all rows from org_client table, selecting only specific columns.

SELECT city, gender, first_name, last_name, email FROM org_client;

Select specific columns and retrieve all rows from org_client.

SELECT city
    gender,
    first_name,
    last_name,
    email
FROM org_client;

Fetch all rows from org_client table, selecting specified columns.

SELECT city, gender, first_name, last_name, email
FROM org_client
ORDER BY first_name;

Choose one column from the org_client table.

SELECT email
FROM org_client;

Example 1:

Let's understand the process of extracting data from a specified table. In this case, we aim to retrieve all rows and columns from the employee table.

Example 1 - Raw data from employee table

25d92dea 1d57 4412 b9a4 c4655adcf42e

Example 1 - Query

SELECT *
FROM org_employee;

Example 1 - Query data mapping

Image Description

In above image, the color green signifies that the output for our query will include all rows and columns being selected.

Example 1 - Query Output

Image Description

Example 2:

Let's understand the process of extracting desired columns from a specified table. In this case, we aim to retrieve 4 columns from the employee table.

Example 2 - Raw data from employee table

Image Description

Example 2 - Query

SELECT employee_id, first_name, last_name, email
FROM org_employee;

Example 2 - Query data mapping

Image Description

In above image, the color green signifies that the output for our query will include data from desired columns(employee_id, first_name, last_name and email).

Example 2 - Query Output

Image Description
Comments(0 comments)

Comments Not Found