๐Ÿ’ป Programming/Oracle 11g

[SQL] COUNT(*) ์™€ COUNT(1)

SQL์—์„œ ํŠน์ • ๋ฐ์ดํƒ€์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฝ‘์•„๋‚ผ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” COUNT()ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

๋ญ ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ์ ์€ ๊ฒฝ์šฐ์—๋Š” ์†๋„์ฐจ์ด๋ฅผ ๋Š๋ผ์ง€ ๋ชปํ•˜๊ฒ ์ง€๋งŒ ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ๋งŽ์€ DB๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์–ด๋–จ๊นŒ...????ํ•˜๋Š” ์˜๋ฌธ์ด ์ƒ๊ธด๋‹ค.

 

์•„์ง ํ™•์‹คํ•œ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด์ง€๋Š” ์•Š์•˜๋‹ค. ๊ทธ์ € ์ธํ„ฐ๋„ท์—์„œ ์ฐพ์€ ์ •๋ณด์— ์˜ํ•˜๋ฉด....

 

์˜์–ด์— ์•ฝํ•œ๋ถ„๋“ค์€ Example๋ถ€๋ถ„์€ ๊ทธ๋ƒฅ ๊ฑด๋„ˆ๋›ฐ๊ณ  Tip: Performance......๋ถ€๋ถ„์„ ๋ณด๋ฉด๋œ๋‹ค. 

================================================================= 

Example - Using SQL GROUP BY Clause

In some cases, you will be required to use the SQL GROUP BY clause with the SQL COUNT function.

For example, you could use the SQL COUNT function to return the name of the department and the number of employees (in the associated department) that make over $25,000 / year.

SELECT department, COUNT(*) AS "Number of employees"
FROM employees
WHERE salary > 25000
GROUP BY department;

Because you have listed one column in your SQL SELECT statement that is not encapsulated in the SQL COUNT function, you must use the SQL GROUP BY clause. The department field must, therefore, be listed in the GROUP BY section.

TIP: Performance Tuning with SQL COUNT

Since the SQL COUNT function will return the same results regardless of what NOT NULL field(s) you include as the SQL COUNT function parameters (ie: within the brackets), you can change the syntax of the SQL COUNT function to COUNT(1) to get better performance as the database engine will not have to fetch back the data fields.

For example, based on the example above, the following syntax would result in better performance:

SELECT department, COUNT(1) AS "Number of employees"
FROM employees
WHERE salary > 25000
GROUP BY department;

Now, the SQL COUNT function does not need to retrieve all fields from the employees table as it had to when you used the COUNT(*) syntax. It will merely retrieve the numeric value of 1 for each record that meets your criteria.

======================================================================== 

 

 

 

์ถœ์ฒ˜ :Http://www.techonthenet.com/sql/count.php