Where Clause in SQL - Retrieve Conditional Records

 We have learned how to select some fields from a table.


It is also possible to retrieve some records.


There is a clause, "where" with which we can specify conditions for a "select" query. That is, we can retrieve some records, only those that meet certain conditions indicated with the "where" clause. For example, we want to see the user whose name is "Marcelo", for this we use "where" and after it, the condition:


 select name, key

  from users

  where name = 'Marcelo';

The basic and general syntax is as follows:


 select FIELDNAME1, ..., FIELDNAMEn

  from TABLE NAME

  where CONDITION;

Relational operators are used for conditions (a topic that we will deal with later in detail). The equal sign (=) is a relational operator. For the following selection of records we specify a condition that requests users whose password is equal to "River":


 select name, key

  from users

  where key = 'River';

If no record meets the condition set with the "where", no record will appear.


So with "where" we set conditions to retrieve some records.


To retrieve some fields from some records, we combine the list of fields and the "where" clause in the query:


 select name

  from users

  where key = 'River';

In the previous query we requested the name of all users whose password is equal to "River".

Comments

Popular posts from this blog

Data Consistency and Inconsistency - What is Difference

Cardinality in DBMS – max/minimum cardinality & examples

Relational Model in DBMS