SQL-basic

SQL, ‘Structured Query Language’, is a programming language designed to manage data stored in relational databases.

  • Relational Databases:
    • a database that organizes information into one or more tables. A table is a collection of data organized into rows and columns. Tables are sometimes referred to as relations.
    • All data stored in a relational database is of a certain data type. Some of the most common data types are:
      1. Integer, a positive or negative whole number
      2. Text, a text string
      3. Date, the date formatted as YYYY-MM-DD for the year, month, and day
      4. Real, a decimal value
  • SQL statement:

    • Statements always end in a semi-colon ;
    • Clauses perform specific tasks in SQL. By convention, clauses are written in capital letters. Clauses can also be referred to as commands. CREATE TABLE (below) is a clause.
    • (column_1 data_type, column_2 data_type, column_3 data_type) is a parameter. A parameter is a list of columns, data types, or values that are passed to a clause as an argument. Here, the parameter is a list of column names and the associated data type.
    • example:

      CREATE TABLE table_name (
      column_1 data_type, 
      column_2 data_type, 
      column_3 data_type
      );
    • Insert: INSERT INTO celebs (id, name, age) VALUES (1, 'Justin Bieber', 21);

    • Select: SELECT name FROM celebs; SELECT DISTINCT is used to return unique values in the result set. It filters out all duplicate values.
    • Update:
      UPDATE celebs
      SET age = 22
      WHERE id = 1;
    • Alter added a new column to the table: ALTER TABLE celebs ADD COLUMN twitter_handle TEXT;
    • Delete: DELETE FROM celebs WHERE twitter_handle IS NULL;
    • LIKE is a special operator used with the WHERE clause to search for a specific pattern in a column. The _ means you can substitute any individual character here without breaking the pattern.

      SELECT * FROM movies
      WHERE name LIKE 'Se_en';
    • % is a wildcard character that matches zero or more missing letters in the pattern.

    • A% matches all movies with names that begin with “A”
    • %a matches all movies that end with “a”
    • Order By: sort the result set by a particular column either alphabetically or numerically. ASC or DESC
    • Limit: specify the maximum number of rows the result set will have.
SELECT * FROM movies
ORDER BY imdb_rating DESC;
  • Aggregate Functions: compute a single result from a set of input values.

    • Count: takes the name of a column as an argument and counts the number of rows where the column is not NULL SELECT COUNT(*) FROM fake_apps;
    • Group By: only used with aggregate functions. It is used in collaboration with the SELECT statement to arrange identical data into groups. Here, our aggregate function is COUNT() and we are passing price as an argument to GROUP BY. SQL will count the total number of apps for each price in the table. SELECT price, COUNT(*) FROM fake_apps
      GROUP BY price;
    • Sum: SELECT SUM(downloads) FROM fake_apps;
    • MAX(): takes the name of a column as an argument and returns the largest value in that column. SELECT MAX(downloads) FROM fake_apps;
    • MIN(): takes the name of a column as an argument and returns the smallest value in that column.
    • AVG() function works by taking a column name as an argument and returns the average value for that column. SELECT AVG(downloads) FROM fake_apps;
    • ROUND(): takes a column name and an integer as an argument. It rounds the values in the column to the number of decimal places specified by the integer.
  • MULTIPLE TABLES:

    • primary key: serves as a unique identifier for each row or record in a given table. The primary key is literally an id value for a record.
    • Foreign Key: a column that contains the primary key of another table in the database. We use foreign keys and primary keys to connect rows in two different tables. One table’s foreign key holds the value of another table’s primary key. Unlike primary keys, foreign keys do not need to be unique and can be NULL.
    • cross join: SELECT albums.name, albums.year, artists.name FROM albums, artists
    • Inner Join:
      SELECT
      *
      FROM
      albums
      JOIN artists ON
      albums.artist_id = artists.id;
    • Left Outer Join: Outer joins also combine rows from two or more tables, but unlike inner joins, they do not require the join condition to be met. Instead, every row in the left table is returned in the result set, and if the join condition is not met, then NULL values are used to fill in the columns from the right table.
      SELECT
      *
      FROM
      albums
      LEFT JOIN artists ON
      albums.artist_id = artists.id;
    • Aliases: AS is a keyword in SQL that allows you to rename a column or table using an alias.
發佈了57 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章