SQL Select Queries

By   Tewodros   Date Posted: Sep. 21, 2021  Hits: 814   Category:  Database   Total Comment: 0             A+ A-


side

Select Query:

GET All Columns and all Rows From a table

select *  from Account

* means all columns

select * means select all columns

from specifies which table

for example: from Account   => this will tell the database to get date from account table

Get Specific Columns

If you want to select only DepositAmount, WithdrawAmount, MerchantName, MerchantAddress columns from account table

select DepositAmount, WithdrawAmount, MerchantName, MerchantAddress

from Account

 

Filter Rows using Where 

where: will allow to filter on rows

To select all columns from account table where the deposit amount is greater than 300 dollars:

select * 

from Account

where DepositAmount > 300

 

Filter Rows and Columns

 

Question: select only four columns (DepositAmount, WithdrawAmount, MerchantName, MerchantAddress) from account table where the deposit amount is greater than 300 dollars:

select DepositAmount, WithdrawAmount, MerchantName, MerchantAddress

from Account

where DepositAmount > 300

 

Compound expression (and, or)

To select only four columns (DepositAmount, WithdrawAmount, MerchantName, MerchantAddress) from account table where the deposit amount is greater than 300 dollars or MerchantName is 'Giant' or MerchantName is 'Lowes'

 

select DepositAmount, WithdrawAmount, MerchantName, MerchantAddress

from Account

where DepositAmount > 300 or MerchantName = 'Giant' or MerchantName = 'Lows'

 

And: to get a true result you need to have all test/expression should return true

Example: Condition: true, false, true, false ===> Result: False

Example: Condition: true, true, true, true   ===> Result: true

Example: Condition: true, true, true, false ===> Result: False

 

OR: to get a true result you need to have at least one of the condition to be true

Example: Condition: true, false, true, false ===> Result: true

Example: Condition: true, true, true, true   ===> Result: true

Example: Condition: false, false, false, false ===> Result: False

 

To select records from account table where deposit amount is greater than 300 and merchant name is either giant or lowes

 

select DepositAmount, WithdrawAmount, MerchantName, MerchantAddress

from Account

where DepositAmount > 300  and (MerchantName = 'Giant' or MerchantName = 'Lows')

 

To select records from account table where deposit amount is greater than 300 and merchant name is giant; or merchant name is lowes

 

select DepositAmount, WithdrawAmount, MerchantName, MerchantAddress

from Account

where (DepositAmount > 300  and MerchantName = 'Giant') or MerchantName = 'Lows'

 

***Do the bracket first when you evaluate true or false

 


Tags



Back to Top



Related Blogs






Please fill all fields that are required and click Add Comment button.

Name:*
Email:*
Comment:*
(Only 2000 char allowed)


Security Code:* alscyq

Back to Top