Knowledge in sql

SQL Wildcard Characters

SQL Wildcard Characters A wildcard character is used to substitute one or more characters in a string. Wildcard characters are used with the SQL LIKE operator. The LIKE operator is used in a WHERE clause to search for a specified pattern in a column

SQL Tutorial

SQL TutorialSQL is a standard language for storing, manipulating and retrieving data in databases.Our SQL tutorial will teach you how to use SQL in: MySQL, SQL Server, MS Access, Oracle, Sybase, Informix, Postgres, and other database systems.Examples in Each ChapterWith our online SQL editor, you can edit the SQL statements, and click on a button to view the result.ExampleSELECT * FROM Customers;

Introduction to SQL

SQL is a standard language for accessing and manipulating databases.What is SQL?SQL stands for Structured Query LanguageSQL lets you access and manipulate databasesSQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the International Organization for Standardization (ISO) in 1987What Can SQL do?SQL can execute queries against a databaseSQL can retrieve data from a databaseSQL can insert records in a databaseSQL can update records in a databaseSQL can delete records from a databaseSQL can create new databasesSQL can create new tables in a databaseSQL can create stored procedures in a databaseSQL can create views in a databaseSQL can set permissions on tables, procedures, and viewsSQL is a Standard - BUT....Although SQL is an ANSI/ISO standard, there are different versions of the SQL language.However, to be compliant with the ANSI standard, they all support at least the major commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.

RDBMS

RDBMS stands for Relational Database Management System.RDBMS is the basis for SQL, and for all modern database systems such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.The data in RDBMS is stored in database objects called tables. A table is a collection of related data entries and it consists of columns and rows.Look at the "Customers" table:ExampleSELECT * FROM Customers;

SQL Syntax

Database TablesA database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders"). Tables contain records (rows) with data.In this tutorial we will use the well-known Northwind sample database (included in MS Access and MS SQL Server).SQL StatementsMost of the actions you need to perform on a database are done with SQL statements.The following SQL statement selects all the records in the "Customers" table:ExampleSELECT * FROM Customers;

SQL SELECT Statement

SQL SELECT StatementThe SQL SELECT StatementThe SELECT statement is used to select data from a database.The data returned is stored in a result table, called the result-set.SELECT SyntaxSELECT column1, column2, ...FROM table_name;Here, column1, column2, ... are the field names of the table you want to select data from. If you want to select all the fields available in the table, use the following syntax:SELECT * FROM table_name;

SQL SELECT DISTINCT Statement

The SQL SELECT DISTINCT StatementThe SELECT DISTINCT statement is used to return only distinct (different) values.Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.SELECT DISTINCT Syntax SELECT DISTINCT column1, column2, ... FROM table_name; SELECT Example Without DISTINCTThe following SQL statement selects ALL (including the duplicates) values from the "Country" column in the "Customers" table:Example SELECT Country FROM Customers;

SELECT DISTINCT Examples

SELECT DISTINCT ExamplesThe following SQL statement selects only the DISTINCT values from the "Country" column in the "Customers" table:Example SELECT DISTINCT Country FROM Customers; The following SQL statement lists the number of different (distinct) customer countries:Example SELECT COUNT(DISTINCT Country) FROM Customers;

SQL WHERE Clause

The SQL WHERE ClauseThe WHERE clause is used to filter records.The WHERE clause is used to extract only those records that fulfill a specified condition.WHERE Syntax SELECT column1, column2, ... FROM table_name WHERE condition; WHERE Clause ExampleThe following SQL statement selects all the customers from the country "Mexico", in the "Customers" table:Example SELECT * FROM Customers WHERE Country='Mexico'; 

SQL AND, OR and NOT Operators

SQL AND, OR and NOT OperatorsThe WHERE clause can be combined with AND, OR, and NOT operators.The AND and OR operators are used to filter records based on more than one condition:The AND operator displays a record if all the conditions separated by AND are TRUE.The OR operator displays a record if any of the conditions separated by OR is TRUE.The NOT operator displays a record if the condition(s) is NOT TRUE.AND Syntax SELECT column1, column2, ... FROM table_name WHERE condition1 AND condition2 AND condition3 ...; OR Syntax SELECT column1, column2, ... FROM table_name WHERE condition1 OR condition2 OR condition3 ...; NOT Syntax SELECT column1, column2, ... FROM table_name WHERE NOT condition;

Example

AND ExampleThe following SQL statement selects all fields from "Customers" where country is "Germany" AND city is "Berlin":Example SELECT * FROM Customers WHERE Country='Germany' AND City='Berlin'; OR ExampleThe following SQL statement selects all fields from "Customers" where city is "Berlin" OR "München":Example SELECT * FROM Customers WHERE City='Berlin' OR City='München'; The following SQL statement selects all fields from "Customers" where country is "Germany" OR "Spain":Example SELECT * FROM Customers WHERE Country='Germany' OR Country='Spain'; NOT ExampleThe following SQL statement selects all fields from "Customers" where country is NOT "Germany":Example SELECT * FROM Customers WHERE NOT Country='Germany'; Combining AND, OR and NOTYou can also combine the AND, OR and NOT operators.The following SQL statement selects all fields from "Customers" where country is "Germany" AND city must be "Berlin" OR "München" (use parenthesis to form complex expressions):Example SELECT * FROM Customers WHERE Country='Germany' AND (City='Berlin' OR City='München'); The following SQL statement selects all fields from "Customers" where country is NOT "Germany" and NOT "USA":Example SELECT * FROM Customers WHERE NOT Country='Germany' AND NOT Country='USA'; 

SQL ORDER BY Keyword

The SQL ORDER BY KeywordThe ORDER BY keyword is used to sort the result-set in ascending or descending order.The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.ORDER BY Syntax SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC|DESC;