Academic Tutorials



English | French | Portugese | German | Italian
Home Advertise Payments Recommended Websites Interview Questions FAQs
News Source Codes E-Books Downloads Jobs Web Hosting
Chats

SQL Server
SQL Server Automating Administration
SQL Server Full Text Searching
SQL Server Installing
SQL Server Selecting XML Data
SQL Server XSL Stylesheets
SQL Server Stored Procedures
SQL Server XDR Schemes
SQL Server Indexed Views
SQL Server Introduction
SQL Server Data Statements
SQL Server SELECT Statement
SQL Server Modification Statements
SQL ServerTransaction Statements
SQL Server Schema Statements
SQL Server XML Introduction
SQL Server XML Templates

HTML Tutorials
HTML Tutorial
XHTML Tutorial
CSS Tutorial
TCP/IP Tutorial
CSS 1.0
CSS 2.0
HLML
XML Tutorials
XML Tutorial
XSL Tutorial
XSLT Tutorial
DTD Tutorial
Schema Tutorial
XForms Tutorial
XSL-FO Tutorial
XML DOM Tutorial
XLink Tutorial
XQuery Tutorial
XPath Tutorial
XPointer Tutorial
RDF Tutorial
SOAP Tutorial
WSDL Tutorial
RSS Tutorial
WAP Tutorial
Web Services Tutorial
Browser Scripting
JavaScript Tutorial
VBScript Tutorial
DHTML Tutorial
HTML DOM Tutorial
WMLScript Tutorial
E4X Tutorial
Server Scripting
ASP Tutorial
PERL Tutorial
SQL Tutorial
ADO Tutorial
CVS
Python
Apple Script
PL/SQL Tutorial
SQL Server
PHP
.NET (dotnet)
Microsoft.Net
ASP.Net
.Net Mobile
C# : C Sharp
ADO.NET
VB.NET
VC++
Multimedia
SVG Tutorial
Flash Tutorial
Media Tutorial
SMIL Tutorial
Photoshop Tutorial
Gimp Tutorial
Matlab
Gnuplot Programming
GIF Animation Tutorial
Scientific Visualization Tutorial
Graphics
Web Building
Web Browsers
Web Hosting
W3C Tutorial
Web Building
Web Quality
Web Semantic
Web Careers
Weblogic Tutorial
SEO
Web Site Hosting
Domain Name
Java Tutorials
Java Tutorial
JSP Tutorial
Servlets Tutorial
Struts Tutorial
EJB Tutorial
JMS Tutorial
JMX Tutorial
Eclipse
J2ME
JBOSS
Programming Langauges
C Tutorial
C++ Tutorial
Visual Basic Tutorial
Data Structures Using C
Cobol
Assembly Language
Mainframe
Forth Programming
Lisp Programming
Pascal
Delphi
Fortran
OOPs
Data Warehousing
CGI Programming
Emacs Tutorial
Gnome
ILU
Soft Skills
Communication Skills
Time Management
Project Management
Team Work
Leadership Skills
Corporate Communication
Negotiation Skills
Database Tutorials
Oracle
MySQL
Operating System
BSD
Symbian
Unix
Internet
IP-Masquerading
IPC
MIDI
Software Testing
Testing
Firewalls
SAP Module
ERP
ABAP
Business Warehousing
SAP Basis
Material Management
Sales & Distribution
Human Resource
Netweaver
Customer Relationship Management
Production and Planning
Networking Programming
Corba Tutorial
Networking Tutorial
Microsoft Office
Microsoft Word
Microsoft Outlook
Microsoft PowerPoint
Microsoft Publisher
Microsoft Excel
Microsoft Front Page
Microsoft InfoPath
Microsoft Access
Accounting
Financial Accounting
Managerial Accounting
Network Sites


SQL-Data Statements


Previoushome Next






SQL-Data Statements


SQL-Data Statements perform query and modification on database tables and columns.

A D V E R T I S E M E N T
This subset of SQL is also called the Data Manipulation Language for SQL (SQL DML).

Contents:

 

SELECT Statement Basics


The SQL SELECT statement queries data from tables in the database. The statement begins with the SELECT keyword. The basic SELECT statement has 3 clauses:

  • SELECT
  • FROM
  • WHERE

The SELECT clause specifies the table columns that are retrieved. The FROM clause specifies the tables accessed. The WHERE clause specifies which table rows are used. The WHERE clause is optional; if missing, all table rows are used.

For example,

    SELECT name FROM s WHERE city='Rome'
    

This query accesses rows from the table - s. It then filters those rows where the city column contains Rome. Finally, the query retrieves the name column from each filtered row. Using the example s table, this query produces:

    name
    Mario

A detailed description of the query actions:

  • The FROM clause accesses the s table. Contents:
      sno name city
      S1 Pierre Paris
      S2 John London
      S3 Mario Rome

  • The WHERE clause filters the rows of the FROM table to use those whose city column contains Rome. This chooses a single row from s:
      sno name city
      S3 Mario Rome

  • The SELECT clause retrieves the name column from the rows filtered by the WHERE clause:
      name
      Mario

The remainder of this subsection examines the 3 major clauses of the SELECT statement, detailing their syntax and semantics:

Extended query capabilities are covered in the next sub-section.



SELECT Clause


The SELECT clause is mandatory. It specifies a list of columns to be retrieved from the tables in the FROM clause. It has the following general format:

    SELECT [ALL|DISTINCT] select-list
    

select-list is a list of column names separated by commas. The ALL and DISTINCT specifiers are optional. DISTINCT specifies that duplicate rows are discarded. A duplicate row is when each corresponding select-list column has the same value. The default is ALL, which retains duplicate rows.

For example,

    SELECT descr, color FROM p
    

The column names in the select list can be qualified by the appropriate table name:

    SELECT p.descr, p.color FROM p
    

A column in the select list can be renamed by following the column name with the new name. For example:

    SELECT name supplier, city location FROM s
    

This produces:

    supplier location
    Pierre Paris
    John London
    Mario Rome

The select list may also contain expressions. See Expressions.

A special select list consisting of a single '*' requests all columns in all tables in the FROM clause. For example,

    sno pno qty
    S1 P1 NULL
    S2 P1 200
    S3 P1 1000
    S3 P2 200

The * delimiter will retrieve just the columns of a single table when qualified by the table name. For example:

    SELECT sp.* FROM sp
    

This produces the same result as the previous example.

An unqualified * cannot be combined with other elements in the select list; it must be stand alone. However, a qualified * can be combined with other elements. For example,

    SELECT sp.*, city
    FROM sp, s
    WHERE sp.sno=s.sno
    

    sno pno qty city
    S1 P1 NULL Paris
    S2 P1 200 London
    S3 P1 1000 Rome
    S3 P2 200 Rome

Note: this is an example of a query joining 2 tables. See Joining Tables.



FROM Clause


The FROM clause always follows the SELECT clause. It lists the tables accessed by the query. For example,

    SELECT * FROM s
    

When the From List contains multiple tables, commas separate the table names. For example,

    SELECT sp.*, city
    FROM sp, s
    WHERE sp.sno=s.sno
    

When the From List has multiple tables, they must be joined together. See Joining Tables.

Correlation Names

Like columns in the select list, tables in the from list can be renamed by following the table name with the new name. For example,

    SELECT supplier.name FROM s supplier
    

The new name is known as the correlation (or range) name for the table. Self joins require correlation names.



WHERE Clause


The WHERE clause is optional. When specified, it always follows the FROM clause. The WHERE clause filters rows from the FROM clause tables. Omitting the WHERE clause specifies that all rows are used.

Following the WHERE keyword is a logical expression, also known as a predicate.

The predicate evaluates to a SQL logical value -- true, false or unknown. The most basic predicate is a comparison:

    color = 'Red'
    

This predicate returns:

  • true -- if the color column contains the string value -- 'Red',
  • false -- if the color column contains another string value (not 'Red'), or
  • unknown -- if the color column contains null.

Generally, a comparison expression compares the contents of a table column to a literal, as above. A comparison expression may also compare two columns to each other. Table joins use this type of comparison. See Joining Tables.

The = (equals) comparison operator compares two values for equality. Additional comparison operators are:

  • > -- greater than
  • < -- less than
  • >= -- greater than or equal to
  • <= -- less than or equal to
  • <> -- not equal to

For example,

    SELECT * FROM sp WHERE qty >= 200
    

    sno pno qty
    S2 P1 200
    S3 P1 1000
    S3 P2 200

Note: In the sp table, the qty column for one of the rows contains null. The comparison - qty >= 200, evaluates to unknown for this row. In the final result of a query, rows with a WHERE clause evaluating to unknown (or false) are eliminated (filtered out).

Both operands of a comparison should be the same data type, however automatic conversions are performed between numeric, datetime and interval types. The CAST expression provides explicit type conversions; see Expressions.


Extended Comparisons

In addition to the basic comparisons described above, SQL supports extended comparison operators -- BETWEEN, IN, LIKE and IS NULL.

  • BETWEEN Operator

    The BETWEEN operator implements a range comparison, that is, it tests whether a value is between two other values. BETWEEN comparisons have the following format:

      value-1 [NOT] BETWEEN value-2 AND value-3
      

    This comparison tests if value-1 is greater than or equal to value-2 and less than or equal to value-3. It is equivalent to the following predicate:

      value-1 >= value-2 AND value-1 <= value-3
      

    Or, if NOT is included:

      NOT (value-1 >= value-2 AND value-1 <= value-3)
      

    For example,

      SELECT *
      FROM sp
      WHERE qty BETWEEN 50 and 500
      

      sno pno qty
      S2 P1 200
      S3 P2 200

     

  • IN Operator

    The IN operator implements comparison to a list of values, that is, it tests whether a value matches any value in a list of values. IN comparisons have the following general format:

      value-1 [NOT] IN ( value-2 [, value-3] ... )
      

    This comparison tests if value-1 matches value-2 or matches value-3, and so on. It is equivalent to the following logical predicate:

      value-1 = value-2 [ OR value-1 = value-3 ] ...
      

    or if NOT is included:

      NOT (value-1 = value-2 [ OR value-1 = value-3 ] ...)
      

    For example,

      SELECT name FROM s WHERE city IN ('Rome','Paris')
      

      name
      Pierre
      Mario

     

  • LIKE Operator

    The LIKE operator implements a pattern match comparison, that is, it matches a string value against a pattern string containing wild-card characters.

    The wild-card characters for LIKE are percent -- '%' and underscore -- '_'. Underscore matches any single character. Percent matches zero or more characters.

    Examples,

     

    Match Value Pattern Result
    'abc' '_b_' True
    'ab' '_b_' False
    'abc' '%b%' True
    'ab' '%b%' True
    'abc' 'a_' False
    'ab' 'a_' True
    'abc' 'a%_' True
    'ab' 'a%_' True
     

    LIKE comparison has the following general format:

      value-1 [NOT] LIKE value-2 [ESCAPE value-3]
      

    All values must be string (character). This comparison uses value-2 as a pattern to match value-1. The optional ESCAPE sub-clause specifies an escape character for the pattern, allowing the pattern to use '%' and '_' (and the escape character) for matching. The ESCAPE value must be a single character string. In the pattern, the ESCAPE character precedes any character to be escaped.

    For example, to match a string ending with '%', use:

      x LIKE '%/%' ESCAPE '/'
      

    A more contrived example that escapes the escape character:

      y LIKE '/%//%' ESCAPE '/'
      

    ... matches any string beginning with '%/'.

    The optional NOT reverses the result so that:

      z NOT LIKE 'abc%'
      

    is equivalent to:

      NOT z LIKE 'abc%'
      

     

  • IS NULL Operator

    A database null in a table column has a special meaning -- the value of the column is not currently known (missing), however its value may be known at a later time. A database null may represent any value in the future, but the value is not available at this time. Since two null columns may eventually be assigned different values, one null can't be compared to another in the conventional way. The following syntax is illegal in SQL:

      WHERE qty = NULL
      

    A special comparison operator -- IS NULL, tests a column for null. It has the following general format:

      value-1 IS [NOT] NULL
      

    This comparison returns true if value-1 contains a null and false otherwise. The optional NOT reverses the result:

      value-1 IS NOT NULL
      

    is equivalent to:

      NOT value-1 IS NULL
      

    For example,

      SELECT * FROM sp WHERE qty IS NULL
      

      sno pno qty
      S1 P1 NULL


Logical Operators

The logical operators are AND, OR, NOT. They take logical expressions as operands and produce a logical result (True, False, Unknown). In logical expressions, parentheses are used for grouping.

  • AND Operator

    The AND operator combines two logical operands. The operands are comparisons or logical expressions. It has the following general format:

      predicate-1 AND predicate-2
      

    AND returns:

     

    • True -- if both operands evaluate to true
    • False -- if either operand evaluates to false
    • Unknown -- otherwise (one operand is true and the other is unknown or both are unknown)

    Truth tables for AND:

     

    AND  T   F   U 
     T   T   F   U 
     F   F   F   F 
     U   U   F   U 
    Input 1 Input 2 AND Result
    True True True
    True False False
    False False False
    False True False
    Unknown Unknown Unknown
    Unknown True Unknown
    Unknown False False
    True Unknown Unknown
    False Unknown False
     

    For example,

      SELECT *
      FROM sp
      WHERE sno='S3' AND qty < 500
      

      sno pno qty
      S3 P2 200

     

  • OR Operator

    The OR operator combines two logical operands. The operands are comparisons or logical expressions. It has the following general format:

      predicate-1 OR predicate-2
      

    OR returns:

     

    • True -- if either operand evaluates to true
    • False -- if both operands evaluate to false
    • Unknown -- otherwise (one operand is false and the other is unknown or both are unknown)

    Truth tables for OR:

     

    OR  T   F   U 
     T   T   T   T 
     F   T   F   U 
     U   T   U   U 
    Input 1 Input 2 OR Result
    True True True
    True False True
    False False False
    False True True
    Unknown Unknown Unknown
    Unknown True True
    Unknown False Unknown
    True Unknown True
    False Unknown Unknown
     

    For example,

      SELECT *
      FROM s
      WHERE sno='S3' OR city = 'London'
      

      sno name city
      S2 John London
      S3 Mario Rome

    AND has a higher precedence than OR, so the following expression:

      a OR b AND c
      

    is equivalent to:

      a OR (b AND c)
      

  • NOT Operator

    The NOT operator inverts the result of a comparison expression or a logical expression. It has the following general format:

      NOT predicate-1
      

    Truth tables for NOT:

     

    NOT  
     T   F 
     F   T 
     U   U 
    Input NOT Result
    True False
    False True
    Unknown Unknown
     

    Example query:

      SELECT *
      FROM sp
      WHERE NOT sno = 'S3'
      

      sno pno qty
      S1 P1 NULL
      S2 P1 200



Be the first one to comment on this page.




  SQL Server eBooks

No eBooks on SQL Server could be found as of now.

 
 SQL Server FAQs
More Links » »
 
 SQL Server Interview Questions
More Links » »
 
 SQL Server Articles
More Links » »
 
 SQL Server News
More Links » »
 
 SQL Server Jobs
More Links » »

Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Netvouz
  • RawSugar
  • Reddit
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

Previoushome Next

Keywords: SQL-Data Statements, SQL Server, SQL Server, SQL Server Tutorial, SQL Server tutorial pdf, history of SQL Server, SQL Server Administration, learn SQL Server

HTML Quizzes
HTML Quiz
XHTML Quiz
CSS Quiz
TCP/IP Quiz
CSS 1.0 Quiz
CSS 2.0 Quiz
HLML Quiz
XML Quizzes
XML Quiz
XSL Quiz
XSLT Quiz
DTD Quiz
Schema Quiz
XForms Quiz
XSL-FO Quiz
XML DOM Quiz
XLink Quiz
XQuery Quiz
XPath Quiz
XPointer Quiz
RDF Quiz
SOAP Quiz
WSDL Quiz
RSS Quiz
WAP Quiz
Web Services Quiz
Browser Scripting Quizzes
JavaScript Quiz
VBScript Quiz
DHTML Quiz
HTML DOM Quiz
WMLScript Quiz
E4X Quiz
Server Scripting Quizzes
ASP Quiz
PERL Quiz
SQL Quiz
ADO Quiz
CVS Quiz
Python Quiz
Apple Script Quiz
PL/SQL Quiz
SQL Server Quiz
PHP Quiz
.NET (dotnet) Quizzes
Microsoft.Net Quiz
ASP.Net Quiz
.Net Mobile Quiz
C# : C Sharp Quiz
ADO.NET Quiz
VB.NET Quiz
VC++ Quiz
Multimedia Quizzes
SVG Quiz
Flash Quiz
Media Quiz
SMIL Quiz
Photoshop Quiz
Gimp Quiz
Matlab Quiz
Gnuplot Programming Quiz
GIF Animation Quiz
Scientific Visualization Quiz
Graphics Quiz
Web Building Quizzes
Web Browsers Quiz
Web Hosting Quiz
W3C Quiz
Web Building Quiz
Web Quality Quiz
Web Semantic Quiz
Web Careers Quiz
Weblogic Quiz
SEO Quiz
Web Site Hosting Quiz
Domain Name Quiz
Java Quizzes
Java Quiz
JSP Quiz
Servlets Quiz
Struts Quiz
EJB Quiz
JMS Quiz
JMX Quiz
Eclipse Quiz
J2ME Quiz
JBOSS Quiz
Programming Langauges Quizzes
C Quiz
C++ Quiz
Visual Basic Quiz
Data Structures Using C Quiz
Cobol Quiz
Assembly Language Quiz
Mainframe Quiz
Forth Programming Quiz
Lisp Programming Quiz
Pascal Quiz
Delphi Quiz
Fortran Quiz
OOPs Quiz
Data Warehousing Quiz
CGI Programming Quiz
Emacs Quiz
Gnome Quiz
ILU Quiz
Soft Skills Quizzes
Communication Skills Quiz
Time Management Quiz
Project Management Quiz
Team Work Quiz
Leadership Skills Quiz
Corporate Communication Quiz
Negotiation Skills Quiz
Database Quizzes
Oracle Quiz
MySQL Quiz
Operating System Quizzes
BSD Quiz
Symbian Quiz
Unix Quiz
Internet Quiz
IP-Masquerading Quiz
IPC Quiz
MIDI Quiz
Software Testing Quizzes
Testing Quiz
Firewalls Quiz
SAP Module Quizzes
ERP Quiz
ABAP Quiz
Business Warehousing Quiz
SAP Basis Quiz
Material Management Quiz
Sales & Distribution Quiz
Human Resource Quiz
Netweaver Quiz
Customer Relationship Management Quiz
Production and Planning Quiz
Networking Programming Quizzes
Corba Quiz
Networking Quiz
Microsoft Office Quizzes
Microsoft Word Quiz
Microsoft Outlook Quiz
Microsoft PowerPoint Quiz
Microsoft Publisher Quiz
Microsoft Excel Quiz
Microsoft Front Page Quiz
Microsoft InfoPath Quiz
Microsoft Access Quiz
Accounting Quizzes
Financial Accounting Quiz
Managerial Accounting Quiz
Testimonials | Contact Us | Link to Us | Site Map
Copyright ? 2008. Academic Tutorials.com. All rights reserved Privacy Policies | About Us
Our Portals : Academic Tutorials | Best eBooksworld | Beyond Stats | City Details | Interview Questions | Discussions World | Excellent Mobiles | Free Bangalore | Give Me The Code | Gog Logo | Indian Free Ads | Jobs Assist | New Interview Questions | One Stop FAQs | One Stop GATE | One Stop GRE | One Stop IAS | One Stop MBA | One Stop SAP | One Stop Testing | Webhosting in India | Dedicated Server in India | Sirf Dosti | Source Codes World | Tasty Food | Tech Archive | Testing Interview Questions | Tests World | The Galz | Top Masala | Vyom | Vyom eBooks | Vyom International | Vyom Links | Vyoms | Vyom World | Important Websites
Copyright ? 2003-2024 Vyom Technosoft Pvt. Ltd., All Rights Reserved.