105.3. SQL data management

Weight: 2

Description: Candidates should be able to query databases and manipulate data using basic SQL commands. This objective includes performing queries involving joining of 2 tables and/or subselects.

Key Knowledge Areas:

  • Use of basic SQL commands

  • Perform basic data manipulation

Terms and Utilities:

  • insert

  • update

  • select

  • delete

  • from

  • where

  • group by

  • order by

  • join

So far in this series of tutorials, we used flat text files to store data. Flat text files can be suitable for small amounts of data, but they are not good for storing large amounts of data or to querying that data. Over the years, several kinds of databases were developed for that purpose, the most common is now the relational database.

Several relational database systems exist today, including commercial products such as Oracle Database and IBM DB2®, and open source projects such as MySQL, PostgreSQL SQLite, and MariaDB (a fork of MySQL). Relational databases use SQL as a data definition and query language.

SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database. it is the standard language for relational database management systems. SQL statements are used to perform tasks such as update data on a database, or retrieve data from a database.

Databases, tables, columns, and rows

A relational database consists of a set of tables. Think of each row of data in the table as a record, with each column of the table corresponding to the fields in the record for the corresponding row. The data in a column is all of the same type— such as character, integer, date, or binary data (such as images).

We are not going to install MySQL or create some databases from zero, our focus is on SQL and we use existing Data Base (ubuntu16.04):

MySQL command line

mysql has simple SQL shell command line which we use to ineteractivly connect to a mysql-server.

it means that I'm going to use u ser root and the password which i will provide. It is also possible to type the password on the command and define which database i am going to use , which is not a great idea!

using a database

After connecting to the mysql-server, we should define which database we are going to use. There might too many databases, first we see them via SHOW DATABASES command and then we use use the define which database we are going to use.

Usually MySQL commands are typed with CAPITAL LETTERS and names and values , ... in lower case. There should be a ; at the end of each MySQL command.

lets see tables:

Exploring tables and columns

SELECT

The select statement is used to query the database and retrieve selected data. We can select everything in the table using * , or we can select from particular columns:

SELECT column1, column2, ... FROM table_name;

WHERE

We can choose which data to display by using the WHERE :

SELECT column1, column2, ... FROM table_name WHERE condition;

it is also possible to make desired condition via AND and OR when using WHERE:

ORDER BY

ORDERBY is used if we want to sort the data based on one field:

SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ...

and it can sort results based on any fields:

GROUP BY

Sometimes you want aggregate information from a table. For example, you want to know how many big hatchbacks you have. We can use the GROUP BY clause to group your data for this purpose:

SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s)

it acts like unique command and group the result , to understand how many rows have been grouped by we can use count:

Creating, changing, and deleting data and tables

INSERT

INSERT command add one or more rows of data to a table.

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

the order is not important and bellow query would have the same result:

UPDATE

UPDATE command to fix this mistake . It update row but which row? we should specified that with WHERE command.

UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;

DELETE

The DELETE statement is used to delete existing records in a table.

DELETE FROM table_name WHERE condition;

JOIN

A JOIN clause is used to combine rows from two or more tables, based on a related column between them. If no related column is given, Every single row from second table (sedan) is copied in front of the first table (hatchback).

and if we give JOIN a common field to JOIN the tables based on that, the magic happens:

Different Types of SQL JOINs

Here are the different types of the JOINs in SQL:

  • (INNER) JOIN: Returns records that have matching values in both tables

  • LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table

  • RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table

  • FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table

finally use quit command to get out of MySQL command line.

that's all!

.

.

.

.

https://developer.ibm.com/tutorials/l-lpic1-105-3/

http://www.sqlcourse.com/intro.html

https://dev.mysql.com/doc/refman/8.0/en/mysql.html

https://jadi.gitbooks.io/lpic1/content/1053_sql_data_management.html

https://www.w3schools.com/sql/sql_select.asp

https://tableplus.com/blog/2018/08/mysql-how-to-turn-off-only-full-group-by.html

.

Last updated

Was this helpful?