SELECT Query

SELECT * FROM students

This command is used to query all the data in the Students table, by typing all the columns in that table instead of *, we get them all.

SELECT
id
,name
,surname
,quiz
,midterm
,final
,class_no
FROM students

We need to specify the column names with commas after the SELECT statement.

FROM indicates where, * indicates all data, SELECT indicates what to do with the obtained data. SELECT is used to select data from that table. If we write the column names specified in the table instead of *, it returns only the information in that column.

Students table

In our example, there are id, name, surname, quiz, midterm, final, class_no columns in the Students table. A total of 7 data has been entered in this table. The first line shows the record with id 1 and name surname Baransel Arslan.

If we want to bring all the information in the name column in the Students table;

select name from students

By typing, we will fetch all the records in the table.

All names on table
SELECT name, surname FROM students

Returns all records in the table as name surname.

All names and surnames on table

When we run the select class_no from Students query, there are 3 values of 149, 603 of 604 as seen in the screenshot below. The reason why there is more than one of the same value is that the class_no is the same for a few students.

All class numbers on table

If we only want the class_no value of 149 to come, we need to use the WHERE conditional clause.

Note: We will talk about WHERE and DISTINCT in detail in our upcoming posts. For now, we use it in examples to better understand SELECT.

e.g;

select * from students where class_no = 149

When we query that, the following values, whose class_no is 149, will return.

All students in class no 149

If we want to add more conditions after the WHERE conditional clause, we can combine them using AND or OR.

When we want to fetch records with class_no 149 AND name Baransel;

select * from students where class_no = 149 AND name = 'Baransel'

If we run it, we get the following result. As you can see, there is only 1 record with name Baransel and class_no 149. With the conditional clause, we can further customize the data we want.

All students in class no 149 and name Baransel

When we want to fetch records with a class_no of 149 OR name Baransel;

select * from students where class_no = 149 OR name = 'Baransel'
All students in class no 149 or name Baransel

There are 2 records. All records whose name is Baransel or whose class_no is 149 are received.

When we want to fetch records with class_no 149 or 603;

select * from students where class_no = 149 OR class_no = 603

can be specified as. But the correct syntax is as follows. If we want to filter the values from the same column and return them as a result of the query, it should be used with the word IN.

select * from students where class_no in (149, 603)

Both query results return the same values, but in should be preferred.

As a different example, if we want to find out how many class_no there are in that table, we need to use DISTINCT

select distinct class_no from students

When we run it, we get the following result.

Distinct SQL Example