BACKEND/DATABASE

SQL 튜토리얼 - Min, Max, Count, Avg, Sum, Like 이해하기

우진하다 2023. 6. 19. 18:27

 

MIN.

 "MIN"은 지정된 테이블의 특정 열에서 최소값을 검색하는 데 사용되는 함수

SELECT MIN(열이름) 
FROM 테이블이름;
-- 전체 상품 
SELECT *
FROM Products;

-- 최소값
SELECT MIN(Price) AS SmallestPrice
FROM Products;

 

MAX.

특정 테이블의 열에서 최대값을 검색하는 데 사용

SELECT MAX(column_name) 
FROM table_name;
SELECT MAX(Price) AS LargestPrice
FROM Products;

 

COUNT.

 지정된 열의 행 수를 계산합니다. 조건에는 특정 필터링 조건을 포함

SELECT COUNT(column_name)
FROM table_name
WHERE condition;
SELECT *
FROM Products;

SELECT COUNT(ProductID)
FROM Products;

SELECT COUNT(ProductID)
FROM Products
WHERE Price > 20;

 

AVG.

특정 열의 평균 값을 계산하는 데 사용

SELECT AVG(column_name)
FROM table_name
WHERE condition;
SELECT AVG(Price)
FROM Products;

SELECT AVG(Price) AS over_20
FROM Products
WHERE Price > 20;

 

SUM.

숫자 열의 총 합계를 반환

SELECT SUM(column_name)
FROM table_name
WHERE condition;
SELECT Quantity
FROM OrderDetails;

SELECT SUM(Quantity)
FROM OrderDetails;

 

LIKE.

패턴 매칭을 수행하기 위해 문자열 비교에 사용됩니다. 
"LIKE" 연산자는 주어진 열의 값이 지정된 패턴과 일치하는지 확인하는 데 사용됩니다. 
패턴은 일반적으로 와일드카드 문자를 포함할 수 있습니다.
 AND 또는 OR연산자를 사용하여 여러 조건을 결합할 수도 있습니다

SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;

다음은 '%' 및 '_' 와일드카드가 있는 다양한 연산자를 보여주는 몇 가지 예

SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';

 

 


출처 : https://www.w3schools.com/sql/sql_like.asp