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.
Most of the actions you need to perform on a db are done with SQL statements.
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 is as follows:
SELECT column1, column2 FROM table_name;
An example of this is:
SELECT age, salary FROM employees;
Where age and salary are both columns in the employees table.
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.
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 is used to filter information in a query statement.
The syntax is as follows:
SELECT column1 FROM table_name WHERE condition;
SELECT * FROM employees WHERE State = "Idaho";
This will return all the information about employees who live in Idaho.
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.
That's it for Part 1. Thanks for reading!