Skip to main content

TOP_LEVEL_DOMAIN

Description​

The TOP_LEVEL_DOMAIN function is used to extract the top-level domain from a URL. If the input URL is invalid, it returns an empty string.

Syntax​

TOP_LEVEL_DOMAIN(<url>)

Parameters​

ParameterDescription
<url>The URL string from which to extract the top-level domain. Type: VARCHAR

Return Value​

Returns VARCHAR type, representing the extracted top-level domain.

Special cases:

  • Returns NULL if url is NULL
  • Returns an empty string if url is not a valid URL format
  • For multi-level domains (e.g., .com.cn), returns the last level domain

Examples​

  1. Basic domain processing
SELECT top_level_domain('www.baidu.com');
+-----------------------------------+
| top_level_domain('www.baidu.com') |
+-----------------------------------+
| com |
+-----------------------------------+
  1. Multi-level domain processing
SELECT top_level_domain('www.google.com.cn');
+---------------------------------------+
| top_level_domain('www.google.com.cn') |
+---------------------------------------+
| cn |
+---------------------------------------+
  1. Invalid URL processing
SELECT top_level_domain('wwwwwwww');
+------------------------------+
| top_level_domain('wwwwwwww') |
+------------------------------+
| |
+------------------------------+