Você está na página 1de 9

8/23/2012 SQLAdvancedQueries(Part1) Day 4 Quick Review

8/23/2012 Database Optimization

Performance Monitoring SQL Profiler Tuning Database Engine Tuning Advisor Performance Monitor

Using SQL Profiler Captures events and activities: SQL Server statements Used for debugging purposes

8/23/2012 Tuning

Adjustments and changes in the database storage organization Requirements for Tuning: Physical independence Means Advanced Queries

Aggregate Functions MIN returns the smallest value in a given column MAX returns the largest value in a given column SUM returns the sum of the numeric values in a given column AVG returns the average value of a given column COUNT returns the total number of values in a given column COUNT(*) returns the number of rows in a table SELECT AVG(Numeric_field) FROM Table; GROUP BY clause SELECT column1, SUM(column2) FROM "list-of-tables" GROUP BY "column-list"

8/23/2012 Advanced Queries -2

HAVING clause SELECT column1, SUM(column2) FROM "list-of-tables" GROUP BY "column-list" HAVING "condition ORDER BY clause SELECT column1, SUM(column2) FROM "list-of-tables" ORDER BY "column-list" [ASC | DESC]; Combining Conditions & Boolean Operators SELECT column1, SUM(column2) FROM "list-of-tables" WHERE "condition1" AND | OR "condition2"; Advanced Queries -3

IN & BETWEEN SELECT col1, SUM(col2) FROM "list-of-tables" WHERE col3 IN (list-of-values); SELECT col1, SUM(col2) FROM "list-of-tables" WHERE col3 BETWEEN value1 AND value2;

8/23/2012 Advanced Queries -4

Mathematical Functions + addition -subtraction * multiplication / division % modulo ABS(x) returns the absolute value of x returns the sign of input x as -1, 0, or 1 (negative, zero, or positive SIGN(x) respectively) MOD(x,y) modulo -returns the integer remainder of x divided by y (same as x%y) FLOOR(x) returns the largest integer value that is less than or equal to x CEILING(x) or returns the smallest integer value that is greater than or equal to x CEIL(x) POWER(x,y) returns the value of x raised to the power of y ROUND(x) returns the value of x rounded to the nearest whole integer returns the value of x rounded to the number of decimal places specified by ROUND(x,d) the value d SQRT(x) returns the square-root value of x

Joins

Inner Join SELECT table1.colum1, table2.column2 FROM table1 INNER JOIN table2 ON table1.ID = table2.ID

8/23/2012 Joins -2 Left outer join Right outer join SELECT * FROM table1 LEFT OUTER JOIN table2 ON table1.ID = table2.ID SELECT * FROM table1 RIGHT OUTER JOIN table2 ON table1.ID = table2.ID Joins -3 Full outer join

SELECT * FROM table1 FULL OUTER JOIN table2 ON table1.ID = table2.ID

8/23/2012 Constraints

Restriction you specify on the data in a database Procedures

Stored procedures contain one or more T-SQL statements, which are compiled and ready to be executed when required. Stored procedures are permanently stored in the database, ready for use at any time

8/23/2012 Demo Tasks Task 1 Using advanced queries Task 2 Adding Constraints Task 3 Creating stored procedrues

Você também pode gostar