How to view a given value in a table

Asked

Viewed 47 times

4

I’m new to the Sql language, and I wanted to show you the names of all the people who are from the RS region, how could I do that ?

create database uri
default character set utf8
default collate utf8_general_ci;


use uri;
create table pessoas(

  id int not null auto_increment,
  nome varchar(30),
  rua varchar(30),
  cidade varchar(30),
  regiao char(3),
  saldo decimal(6,2),
  primary key(id)

) default charset utf8;

insert into pessoas values
 ('1','Pedro Augusto da Rocha','Rua Pedro Carlos Hoffman','Porto Alegre','RS','700,00'),
 ('2','Antonio Carlos Mamel','Av. Pinheiros','Belo Horizonte','MG','3500,50'),
 ('3','Luiza Augusta Mhor','Rua Salto Grande','Niteroi','RJ','4000,00'),
 ('4','Jane Ester','Av 7 de setembro','Erechim','RS','800,00'),
 ('5','Marcos Antônio dos Santos','Av Farrapos','Porto Alegre','RS','4250,25');

3 answers

4


With this SQL:

SELECT nome FROM pessoas WHERE regiao = 'RS'

Explanation: you are selecting (SELECT) the name (nome) of people (table pessoas) in which (WHERE) the region (regiao) is equal (=) a RS.

I suggest changing the name of the column regiao for estado or uf, because region refers to Southeast, South, etc.

3

You can use the following query

SELECT 
    pessoas.nome,
    pessoas.regiao
FROM pessoas WHERE regiao = 'RS'

To make filters in sql use the clause WHERE, as shown above.

Note also that in query I returned only the name and the region, to return all the data, you can use the *

SELECT 
    *
FROM pessoas WHERE regiao = 'RS'

2

Just use the WHERE to filter your query.

WHERE: Specifies the search criteria for the rows returned by the query.

Sqlfiddle - Online Example:

SELECT 
  * 
FROM 
  Pessoas
WHERE
  Regiao = 'RS'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.