What is function in sql? Explain group functions and string function with example.

Group Functions in SQL

In SQL, group functions help us perform calculations on groups of rows in a database table. These functions allow us to summarize and retrieve information in a meaningful way. Here are some common group functions with examples and SQL codes:

Various Group Functions

  1. COUNT()
    • Counts the number of rows in a group.Example: Count the number of employees in a department.
    SELECT department, COUNT(*) FROM employees GROUP BY department;
  2. SUM()
    • Calculates the sum of values in a group.Example: Calculate the total salary of employees in a department
    SELECT department, SUM(salary) FROM employees GROUP BY department;
  3. AVG()
    • Computes the average value of a column in a group.Example: Find the average age of customers in a city.
    SELECT city, AVG(age) FROM customers GROUP BY city;
  4. MAX()
    • Retrieves the maximum value in a group.Example: Find the highest score in each subject.
    SELECT subject, MAX(score) FROM exam_scores GROUP BY subject;
  5. MIN()
    • Retrieves the minimum value in a group.Example: Find the lowest temperature in each city.
    SELECT city, MIN(temperature) FROM weather_data GROUP BY city;

Summary

  • Group functions in SQL help us perform calculations on groups of data.
  • COUNT() counts rows, SUM() adds up values, AVG() finds the average, MAX() gets the highest, and MIN() retrieves the lowest value.
  • They are often used with the GROUP BY clause to group data by a specific column.
  • These functions are useful for generating meaningful insights and reports from large datasets.
FunctionDescriptionExample
COUNT()Count rows in a groupSELECT department, COUNT(*) FROM employees GROUP BY department;
SUM()Sum values in a groupSELECT department, SUM(salary) FROM employees GROUP BY department;
AVG()Calculate average in a groupSELECT city, AVG(age) FROM customers GROUP BY city;
MAX()Find maximum in a groupSELECT subject, MAX(score) FROM exam_scores GROUP BY subject;
MIN()Find minimum in a groupSELECT city, MIN(temperature) FROM weather_data GROUP BY city;

String Functions in SQL

String functions in SQL are special operations that allow you to manipulate and work with text or character data in a database. These functions help you perform tasks like extracting specific parts of a string, changing the case of text, or combining multiple strings together.

Here are some common string functions in SQL, along with examples and SQL code:

1. LEN()

  • Purpose: Calculates the length (number of characters) of a string.
  • Example:SELECT LEN('Hello, World!') AS Length; Output: Length: 13

2. UPPER()

  • Purpose: Converts all characters in a string to uppercase.
  • Example:SELECT UPPER('Hello, World!') AS Uppercase; Output: Uppercase: HELLO, WORLD!

3. LOWER()

  • Purpose: Converts all characters in a string to lowercase.
  • Example:SELECT LOWER('Hello, World!') AS Lowercase; Output: Lowercase: hello, world!

4. SUBSTRING()

  • Purpose: Extracts a portion of a string based on the specified starting position and length.
  • Example:SELECT SUBSTRING('Hello, World!', 7, 5) AS Substring; Output: Substring: World

5. CONCAT()

  • Purpose: Combines two or more strings together.
  • Example:SELECT CONCAT('Hello', ', ', 'World!') AS Concatenated; Output: Concatenated: Hello, World!

6. REPLACE()

  • Purpose: Replaces all occurrences of a specified substring with another substring.
  • Example:SELECT REPLACE('Hello, World!', 'World', 'Universe') AS Replaced; Output: Replaced: Hello, Universe!

7. TRIM()

  • Purpose: Removes leading and trailing spaces from a string.
  • Example:SELECT TRIM(' Hello, World! ') AS Trimmed; Output: Trimmed: Hello, World!

8. CHARINDEX()

  • Purpose: Finds the position of a substring within a string.
  • Example:SELECT CHARINDEX('World', 'Hello, World!') AS Position; Output: Position: 7

Summary:

FunctionPurposeExample
LEN()Calculate string lengthSELECT LEN('Hello, World!')
UPPER()Convert to uppercaseSELECT UPPER('Hello, World!')
LOWER()Convert to lowercaseSELECT LOWER('Hello, World!')
SUBSTRING()Extract part of a stringSELECT SUBSTRING('Hello, World!', 7, 5)
CONCAT()Combine stringsSELECT CONCAT('Hello', ', ', 'World!')
REPLACE()Replace text in a stringSELECT REPLACE('Hello, World!', 'World', 'Universe')
TRIM()Remove leading/trailing spacesSELECT TRIM(' Hello, World! ')
CHARINDEX()Find position of substringSELECT CHARINDEX('World', 'Hello, World!')

String functions in SQL are handy tools for working with text data, allowing you to manipulate and extract information from strings stored in your database.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *