website logo
⌘K
🏠Homepage
👋Introduction
Key Concepts & Architecture
🚀Getting Started
👨‍💻SQL Reference
SQL Queries
SQL Clauses
SQL Data Types
SQL Functions
Schema
🚨Error Handling
🆚Differences Between Oxla vs. PostgreSQL
Docs powered by archbee 

SELECT statement

16min

The SELECT statement is the most commonly used query. You can use it to access records and retrieve specified data. This section explains how to use SELECT with syntax and some use cases.

Overview

The SELECT query helps you obtain the data you need from one or more tables.

The application of this query will be helpful in several cases listed below:

  • Evaluating data from only particular fields in a table.
  • Reviewing data from several tables at the same time.
  • Retrieving the data based on specific criteria.

Syntax

To request data from a table using the SELECT query, you can use the following syntax:

Syntax
|
SELECT * FROM table_name;


You are allowed to filter the table by column. Refer to the syntax below.

Syntax
|
SELECT column1, column2, ...
FROM table_name;


We will define each syntax as follows.

  • SELECT determines the data we need from the database or a table.
  • * referred to as select star or asterisk or represents all. It defines that the query should return all columns of the queried tables.
  • FROM clause indicates the table(s) to retrieve data from.
  • table_name represents the table(s) name.
  • column1, column2, ... these are used to specify the columns from where we want to retrieve the data.

ℹ️ The SELECT query is case insensitive, which means select or SELECT has the same result.

Examples

We have a table named student_data that stores the id, name, and where the student lives.

Create table
|
CREATE TABLE student_data (
  id int,
  name string,
  domicile string
);
INSERT INTO student_data 
    (id, name, domicile) 
VALUES 
    (119291,'Jordan','Los Angeles'),
    (119292,'Mike','Melbourne'),
    (119293,'Will','Sydney');


All the examples below are executed in the public schema. You can also display table from another specific schema. Click here for more info.

#Case 1: Query data from all columns

1) In the first case, we want to display all the data from the student_data table. Please refer to the syntax below:

Select query
|
SELECT * FROM table_name;


2) Use the select query within the table name to get all the data:

Select query
|
SELECT * FROM student_data;


3) If you have successfully run the query, you will get all the data from the student_data table.

Final output
|
+--------+----------+----------------+
| id     | name     | domicile       |
+--------+----------+----------------+
| 119291 | Jordan   | Los Angeles    | 
| 119292 | Mike     | Melbourne      |
| 119293 | Will     | Sydney         |
+--------+----------+----------------+


#Case 2: Query data from specific columns

1) We want to get the list of students' names with their IDs. Please refer to the syntax below:

Syntax
|
SELECT column_1, column_2 FROM table_name;


2) Run the following query:

Select query
|
SELECT id, name FROM student_data;


3) If you have successfully run the query, you will get a list of students' IDs & names from the student_data table.

Final output
|
+--------+----------+
| id     | name     | 
+--------+----------+
| 119291 | Jordan   | 
| 119292 | Mike     |
| 119293 | Will     | 
+--------+----------+


#Case 3: Query data from a specific column with the condition

1) If we have a large number of data, skimming for the desired data will require a long time. We can apply some conditions to the SELECT query. Please refer to the syntax below:

Syntax
|
SELECT column_1 FROM table_name WHERE condition;


2) Let's say we want to know the student's name who lives in Sydney, have a look and run the query below:

Select query
|
SELECT name FROM student_data WHERE domicile='Sydney';


3) If you have successfully run the query, we now know that Will lives in Sydney! 🇦🇺

Final output
|
+----------+ 
| name     | 
+----------+
| Will     | 
+----------+




Updated 11 May 2023
Did this page help you?
Yes
No
PREVIOUS
CREATE INDEX statement
NEXT
DROP statement
Docs powered by archbee 
TABLE OF CONTENTS
Overview
Syntax
Examples
#Case 1: Query data from all columns
#Case 2: Query data from specific columns
#Case 3: Query data from a specific column with the condition

Dedicated to data analytics that provides 10x faster execution of analytical queries than current state of the art solutions. We are launching SOON! Join the waiting list for more info.




©2022 Oxla