How to Query a Value in a Table

4

I'm new to the Sql language, and I wanted to show the name of all 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');
    
asked by anonymous 27.04.2018 / 20:14

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 equal ( regiao ) to RS.

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

    
27.04.2018 / 20:17
3

You can use the following query

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

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

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

SELECT 
    *
FROM pessoas WHERE regiao = 'RS'
    
27.04.2018 / 20:19
2

Just use 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'
    
27.04.2018 / 20:21