Professional Documents
Culture Documents
The TOP option is used for limiting the output of a query result set. TOP can
either specify the number of rows to return, or the percentage of rows to return.
The ORDER BY clause can be used successfully with the TOP option.
SELECT TOP 10 *
FROM employees
SELECT TOP 10 *
FROM employees
ORDER BY Salary DESC
INTO clause creates a new table and inserts rows and columns listed in the
SELECT statement into it. INTO clause also inserts existing rows into a new
table.
2
[ ] (Wildcard - Character(s) to Match)
3
[^] (Wildcard - Character(s) Not to Match)
Matches any single character that is not within the range or set
specified between the square brackets.
4
GROUP BY ALL
If you use ALL, the query results include all groups produced by the
GROUP BY clause, even if some of the groups have no rows that meet
the search conditions. Without ALL, a SELECT statement that includes
GROUP BY does not show groups for which no rows qualify.
SELECT AVG(SAL),DEPTNO
FROM EMP
WHERE DEPTNO IN(10,20)
GROUP BY ALL DEPTNO
5
GROUP BY CUBE
SELECT AVG(SAL),DEPTNO,JOB
FROM EMP
GROUP BY DEPTNO ,JOB WITH CUBE
6
GROUP BY ROLLUP
SELECT AVG(SAL),DEPTNO,JOB
FROM EMP
GROUP BY DEPTNO,JOB WITH ROLLUP
7
SQL Insert
8
SQL Update
UPDATE student
SET age = 12
WHERE rollno = 1
9
SQL Delete
The SQL DELETE statement allows you to delete a record from the database.
The DELETE command uses a WHERE clause. If you don't use a WHERE
clause, all rows in the table will be deleted.
10
Data type Range Storage
-2^63 (-9,223,372,036,854,775,808) to
bigint 8 Bytes
2^63-1 (9,223,372,036,854,775,807)
11
Fixed-precision and scale-numeric data from –10^38+1 through 10^38–1.
numeric (p, s)
Storage size is 19 bytes.
12
datetime Date and time data from January 1, 1753, to December 31, 9999,
13
Fixed-length, non-Unicode character data with a length of n bytes. n
char [ ( n ) ]
must be a value from 1 through 8,000.
14
national character(n)
Fixed-length Unicode data with a maximum length of 4000 characters.
Synonym: nchar(n)
national character
varying(n) Variable-length Unicode data with a length of 1 to 4000 characters.
Synonym: nvarchar(n)
15
Variable-length Unicode data with a maximum length of (2^30–2)/2
ntext
(536,870,911) characters.
16
Only data columns of the integer data types can be used for identity columns. A table can
have only one identity column. A seed and increment can be specified and the column
IDENTITY [(s, i)] cannot be updated.
s (seed) = starting value
i (increment) = increment value
This is a property of a data column, not a distinct data type. It is a column in a table that is
ROWGUIDCOL defined by using the uniqueidentifier data type. A table can have only one ROWGUIDCOL
column.
17
Using the IDENTITY property with CREATE TABLE
18
Naming Conventions
DEPT30
EMPNO ENAME ANNSAL HIREDATE JOB
------ ---------- --------
7698 BLAKE 34200 01-MAY-81
7654 MARTIN 15000 28-SEP-81
7499 ALLEN 19200 20-FEB-81
7844 TURNER 18000 08-SEP-81
...
Adding a Column
PRIMARY KEY
DEPT
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
DEPT
PRIMARY DEPTNO DNAME LOC
KEY ------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
...
EMP
EMPNO ENAME JOB ... COMM DEPTNO FOREIGN
KEY
7839 KING PRESIDENT 10
7698 BLAKE MANAGER 30
...
Not allowed
(DEPTNO 9
Insert into does not exist in
the DEPT table
7571 FORD MANAGER ... 200 9
7571 FORD MANAGER ... 200 Allowed
The FOREIGN KEY Constraint