DEV Community

Cover image for SQL Review - WHERE, SELECT
Jo
Jo

Posted on • Updated on

SQL Review - WHERE, SELECT

I've recently started teaching myself SQL as part of the #100daysofcode challenge and I thought I would make some notes in order to review some of the things I've learnt.
SQl is the language used for storing, manipulating and retrieving data. The SQL Language is used with MySQL, SQL Server, MS Access, Oracle and other systems.

SQL stands for Structured Query Language. It can execute, retrieve, insert, update, delete and create records in a table in a database.

SQL Statements

Most of the actions you need to perform on a db are done with SQL statements.

Ex.
SELECT * FROM employees;
This line of code is using the SELECT statement to query all the column names from the employees table using the * (which means all columns)

SQL keywords are not case sensitive, so SELECT is the same as select, but it is common practice to to capitalize keywords.

SELECT SYNTAX

SELECT Syntax is as follows:

SELECT column1, column2 FROM table_name;

An example of this is:

SELECT age, salary 
FROM employees;
Enter fullscreen mode Exit fullscreen mode

Where age and salary are both columns in the employees table.

SELECT DISTINCT

The SELECT DISTINCT keyword is used to retrieve unique values in a table, so values will not be repeated in the result set. Since a column can often have duplicate values, using the DISTINCT keyword is a good way of removing those duplicates.

For example, customers may purchase the same kind of fruit. You may just want the data for the kind of fruit purchased, and not necessarily all data for all the fruit each individual purchased.

Instead of typing SELECT Fruit FROM Customers;
We would type:
SELECT DISTINCT Fruit FROM Customers;

We can get even more specific while using the DISTINCT keyword.
For ex:

SELECT COUNT (DISTINCT Fruit) FROM Customers;

As the keyword COUNT may suggest, this query will count the number of distinct values in a column and return that value to you.

The WHERE Clause

The WHERE clause is used to filter information in a query statement.
The syntax is as follows:
SELECT column1 FROM table_name WHERE condition;

For ex:

SELECT * FROM employees
WHERE State = "Idaho";
Enter fullscreen mode Exit fullscreen mode

This will return all the information about employees who live in Idaho.

Text and Numeric Fields

You must use single or double quotes around text fields in SQL. The opposite is true for numeric fields, as they do not require quotes and will result in an error.

Note: You must have a semicolon at the end of each query for it to run. You can't just do whatever, this isn't javascript ;)

That's it for Part 1. Thanks for reading!

Top comments (0)