In today’s article, we will cover What is Select and How to Write a Basic Select Statement.
Select is a postgresql sentence used to convey the data in the desired column on the Table to the user.
To explain through an example, you went to the grocery store and there are a lot of fruits and vegetables, but you only want to buy apples and cherries, so you choose apples and cherries from among the many products and tell the grocery store how many kg you will buy.
The same logic in PostgreSQL, you can select the data in the table according to the column you want and return it to you.
The usage is as follows:
1 |
SELECT *FROM Table_name |
Or:
1 |
SELECT Column1, Column2 FROM Table_name |
When using the SELECT clause, you can select the columns you want to see the data for and write between SELECT and FROM.
If you are writing more than one column, you need to put a period between them.
But I want to see the data in all columns and do I have to write them all one by one?
No, it will be enough to write * between SELECT and FROM for this.
In the table below, we pull all the records in the Name and Surname columns in the Personnel table.
1 |
SELECT Name,Surname From Personel |
We can make various changes on the data with the Select operation.
In the example below, we have written a select clause to see the sum of the vize and final exam grades of the students in the student table.
1 |
select name, vize+final from student; |
If you only want to add two numbers, or just want to print text, you can do as in the examples below.
1 |
select 'Faruk ERDEM' |
1 |
select 123 + 345 |
Example:
We did the following example using *, and since our table has 3 records and 3 columns, we have 3 columns and 3 records.
If we want to extract only the name and vize columns, we need to do as follows.