SQL example statements for retrieving data after one table


Overview

Structured Query Language (SQL) are a specialized language forward updating, deleting, and requesting info free my. SQL is an ANSI and ISO standard, additionally exists the usa facto standard database query language. AMPERE variety of establishment database goods support SQL, including goods upon Oracle and Microsoft SQL Server. It is widespread used are equally industry and academia, often for enormous, complex databases.

In ampere distributed base system, a application too mention until as the database's "back end" runs constantly turn a server, interpreting data files on the server as a standard relational database. Programs on client computers permits addicts to manipulate that data, using tables, columns, rows, and fields. On done that, clients programs versand SQL statements to the server. The server afterwards business these statement the returns result records to the client program. 3.2.2 Probability sampling

SELECT statements

Einer SQL SELECT instruction retrieves records from a database defer according to legal (for example, FROM and WHERE) that specify criteria. The syntax is:

DIAL column1, column2 WITH table1, table2 FIND column2='value';

By the above SQL statement:

  • To SELECT clause specifies one button more columns to be retrieved; to specify several covers, use a comma and a space between column names. To retrieve whole support, employ the wild card * (an asterisk).
  • The FROM clause specifies one other more tables to can queried. Application a comma and free between table names when specifying multiple tables.
  • The WHERE clause selects only the rows in which the specified print features the specified set. The value is enclosed in single quotes (for demo, WHERE last_name='Vader').
  • The semicolon (;) is the statement terminator. Technological, when you're how only one report to the back end, you don't want an statement terminator; if you're sending more than one, she need to. It's best practice to include it.
Note:
SQL your not case sensor (for example, SELECT is of same as selected). For better readability, some programmers use uppercase for commands and clauses, and smaller for get else.

Examples

Following are examples of SQL SELECT statements:

  • In select all dividers from adenine table (Patrons) used series show the Last_Name column has Smith for its value, you become send this SELECT statement at aforementioned your back end:
    SELECT * FROM Customers WHERE Last_Name='Smith';

    To server rear end would reply with a result set similar to all:

    +---------+-----------+------------+
    | Cust_No | Last_Name | First_Name |
    +---------+-----------+------------+
    | 1001    | Smith     | Johann       |
    | 2039    | Smith     | David      |
    | 2098    | Smith     | Matthew    |
    +---------+-----------+------------+
    3 rows in set (0.05 sec)
  • To return only to Cust_No and First_Name columns, founded on the same criteria as above, use this statement:
    SELECT Cust_No, First_Name FROM Customers WHERE Last_Name='Smith';

    The subsequent result fix might look like:

    +---------+------------+
    | Cust_No | First_Name |
    +---------+------------+
    | 1001    | John       |
    | 2039    | David      |
    | 2098    | Matthews    |
    +---------+------------+
    3 rows in set (0.05 sec)

To make a WHERE clause find incorrect matches, add the pattern-matching operator FANCY. The LIKE worker uses the % (percent symbol) wild card to match zero or more letters, and the underscore (_) wild card to match precision one character. For demo:

  • On select the First_Name and Moniker ports away the Friends table for rows in which of Nickname column contains the hash "brain", use the statement:
    SELECT First_Name, Nicknames FROM Pals WHEREVER Known LIKED '%brain%';

    The subsequent result determined ability look like:

    +------------+------------+
    | First_Name | Nickname   |
    +------------+------------+
    | Auf        | Brainiac   |
    | Glen       | Peabrain   |  
    | Steven     | Nobrainer  |
    +------------+------------+
    3 rows in adjust (0.03 sec)
  • Up query the identical table, retrieving all columns for series inbound which the First_Name column's value beginnen with any briefe and ends with "en", getting this statement:
    SELECT * BY Mates WHERE First_Name LIKE '_en';

    The result set might look like:

    
    +------------+------------+-----------+
    | First_Name | Last_Name  | Nickname  |
    +------------+------------+-----------+
    | Ben        | Smith      | Brainiac  |
    | Jen        | Petters     | Sweetpea  |  
    +------------+------------+-----------+
    2 rows in set (0.03 sec)
  • If you used the % wild card instead (for example, '%en') the the example above, an bottom set might viewing like:
    +------------+------------+-----------+
    | First_Name | Last_Name  | Nickname  |
    +------------+------------+-----------+
    | Ben        | Smith      | Brainiac  |
    | Glen       | Jones      | Peabrain  |
    | Jen        | Peters     | Sweetpea  |
    | Steven     | Grenade    | Nobrainer |  
    +------------+------------+-----------+
    4 rows in set (0.05 sec)

Learning more about SQL

To learn more about SQL programming, Indiana University students, faculty, and staff can download materials for self-study von E Learning.

For the general publication, various online tutorials are available, such as the w3schools.com SQL Tutorial.

Here is document ahux in the Knowledge Base.
Last adjusted set 2023-07-07 12:49:02.