In this tutorial we will be exploring the select statement for querying the database.
It is one of the most complex statement in PostgreSQL because of many number of clauses it provide for writing flexible queries.
Basic Syntax
SELECT
select_list(here you define needed attributes)
FROM
table_name;
we separate column/attributes by ,
. Incase we need all the available columns in table, instead of writing all columns name separated by ,
, we use *
.
Select statement selects mentioned columns from the table mentioned in from statement.
Note: From clause is not mandatory. if you do not need retrieve data from any table, you can just use Select statement.
Also, all statements are case insensitive in PostgreSQL. Hence select is same as SELECT or Select.
Evaluation flow:
As, you can see from clause is executed before select clause.
Different Clauses of SELECT
- DISTINCT: It is used to Select distinct rows.
- ORDER BY: It is used to Sort rows.
- WHERE: It is used to Filter rows.
- LIMIT or FETCH: They are used to Select a subset of rows from a table.
- GROUP BY: It is used to Group rows.
- HAVING: It is used to Filter groups.
-
Join: There are many types of joins like
INNER JOIN
,LEFT JOIN
,FULL OUTER JOIN
andCROSS JOIN
. All of them are used to join tables. -
Set operations: Different set operations can be performed using
UNION
,INTERSECT
, andEXCEPT
.
Practical Examples
Code
SELECT first_name FROM customer;
Output
Code
SELECT
first_name,
last_name,
email
FROM
customer;
Output
Code
SELECT * FROM customer;
Output
That's it for today!
Feel free to reach me out, if you got any confusion.
Thanks for reading!
Top comments (0)