Teaching Kids Programming – Introduction to SQL and the SELECT

Teaching Kids Programming – Introduction to SQL and the SELECT | ninjasquad

Teaching Kids Programming: Videos on Data Structures and Algorithms

The SQL (Structured Query Language) is a programming language that is used to interact with the Database. The SELECT statement is used to retrieve (and filter) data from Database (which contains the Organised data).

A Database con contain many tables. A table in a RDMS (Relational Database Management System) contains rows and columns. The columns are attributes (meta data), the rows are the actual data.

Given the following table named Person

```Name | Age | Gender | Score
---------------------------
Eric | 10  | Male   | 90
---------------------------
Ryan | 8   | Male   | 85
---------------------------
Yan  | 9   | Female | 87
---------------------------```

We have columns “Name”, “Age”, “Gender” and “Score”. We can add indexes to columns. The indices are used to speed up searches. If there is no indices or primary keys of a table, the database has to go through each row to find the matched records.

A table can only have a primary key which is a unique index.

The basic SELECT statement is:

```SELECT something
FROM a_table
WHERE some_conditions
ORDER BY some_columns```

The where and order by clause are optional.

To select everything, we use “*” (wild chars or asterisk). We can select multiple columns, separated by “,”

We can select from multiple tables (we can join the tables by some common columns).

The where clause is to specify the filter criteria.

We can apply aggregate functions such as max, min, avg, sum on the columns.

The order-by clause can specify one or more columns for results to be sorted. By Default, the order is ascending (ASC), and we can specify the DESC to sort the result in descending order.

Here are a few examples of SQL:

SQL to Get the name and score of person who is age 10:

```SELECT Name, Score
FROM Persons
WHERE Age = 10```

SQL to Get the max age:

```SELECT Max(Age)
FROM Persons```

SQL to Sort the Table by Score Descending order:

```SELECT *
FROM Persons
ORDER BY Score DESC```

GD Star Rating