CODE

Cards (70)

  • SELECT
    Extracts data from a database
  • UPDATE
    Updates data in a database
  • DELETE
    Deletes data from a database
  • INSERT INTO

    Inserts new data into a database
  • CREATE DATABASE
    Creates a new database
  • ALTER DATABASE
    Modifies a database
  • CREATE TABLE

    Creates a new table
  • ALTER TABLE

    Modifies a table
  • DROP TABLE

    Deletes a table
  • CREATE INDEX

    Creates an index (search key)
  • DROP INDEX

    Deletes an index
  • SELECT statement

    Used to select data from a database
  • SELECT * syntax

    Used to return all columns, without specifying every column name
  • SELECT DISTINCT statement

    Used to return only distinct (different) values
  • WHERE clause
    Used to filter records, to extract only those records that fulfill a specified condition
  • ORDER BY keyword

    Used to sort the result-set in ascending or descending order
  • ORDER BY keyword (default)

    Sorts the records in ascending order by default
  • ORDER BY DESC keyword

    Used to sort the records in descending order
  • ORDER BY keyword (strings)

    Orders alphabetically for string values
  • ORDER BY DESC keyword (strings)

    Used to sort the table reverse alphabetically
  • Select all customers from Spain that starts with the letter 'G':

    SELECT *FROM CustomersWHERE Country = 'Spain' AND CustomerName LIKE 'G%';
  • The AND operator displays a record if all the conditions are TRUE.
    The OR operator displays a record if any of the conditions are TRUE.
  • Select all fields from Customers where Country is "Germany" AND City is "Berlin" AND PostalCode is higher than 12000:

    SELECT * FROM CustomersWHERE Country = 'Germany'AND City = 'Berlin'AND PostalCode > 12000;
  • SELECT all customers from Spain that starts EITHER a "G" or an "R".

    SELECT * FROM CustomersWHERE Country = 'Spain' AND (CustomerName LIKE 'G%' OR CustomerName LIKE 'R%');
  • Sort the products by price:
    SELECT * FROM ProductsORDER BY Price;
  • Select all customers that either:are from Spain and starts with either "G", orstarts with the letter "R":

    SELECT * FROM CustomersWHERE Country = 'Spain' AND CustomerName LIKE 'G%' OR CustomerName LIKE 'R%';
  • Sort the products from highest to lowest price:

    SELECT * FROM ProductsORDER BY Price DESC;
  • Sort the products alphabetically by ProductName:
    SELECT * FROM ProductsORDER BY ProductName;
  • SELECT * FROM Products ORDER BY ProductName DESC;
    Sort the products by ProductName in reverse order:
  • select all customers from the "Customers" table, sorted by the "Country" and the "CustomerName" column. This means that it orders by Country, but if some rows have the same Country, it orders them by CustomerName:

    SELECT * FROM CustomersORDER BY Country, CustomerName;
  • The following SQL statement selects all customers from the "Customers" table, sorted ascending by the "Country" and descending by the "CustomerName" column:
    SELECT * FROM CustomersORDER BY Country ASC, CustomerName DESC;
  • Select all customers from COUNTRY Mexico:

    SELECT * FROM CustomersWHERE Country='Mexico';
  • Select all customers with a CustomerID greater than 80:
    SELECT * FROM CustomersWHERE CustomerID > 80;
  • Select all the different countries from the "Customers" table:
    SELECT DISTINCT Country FROM Customers;
  • Select all countries entered into the "Customers" table:
    SELECT Country FROM Customers;
  • By using the DISTINCT keyword in a function called COUNT, we can return the number of different countries.

    SELECT COUNT(DISTINCT Country) FROM Customers;
  • Select all records from the Customers table:
    SELECT * FROM Customers;
  • Select all customers from Germany or Spain:

    SELECT * FROM CustomersWHERE Country = 'Germany' OR Country = 'Spain';
  • The OR operator displays a record if any of the conditions are TRUE.
    The AND operator displays a record if all the conditions are TRUE.
  • SELECT * FROM CustomersWHERE City = 'Berlin' OR CustomerName LIKE 'G%' OR Country = 'Norway';

    The following SQL statement selects all fields from Customers where either City is "Berlin", CustomerName starts with the letter "G" or Country is "Norway":