The SQL queries listed below can be very useful for SharePoint Farm administrators to manage/administer the SharePoint content/config database as well as the service application databases.
Read my previous article on some tools for SharePoint:
Note: The queries mentioned below are not specific only to SharePoint databases and can be used with any SQL database.
- To get the total number of space utilized by all the SharePoint databases in SQL server:
- SELECT CONVERT(DECIMAL(10,2),(SUM(size * 8.00) / 1024.00 / 1024.00)) As UsedSpace
- FROM master.sys.master_files
- To get the name of all the SharePoint databases in a SQL instance:
- Select * from Sys.Databases
- To get the total number of space utilized by all the SharePoint databases in SQL server:
- SELECT CONVERT(DECIMAL(10,2),(SUM(size * 8.00) / 1024.00 / 1024.00)) As UsedSpace
- FROM master.sys.master_files
- To find the space used by a SharePoint DB and its free size:
- Replace MY_DB with the concerned database name
- use "MY_DB"
- exec sp_spaceused
- To find the size consumed by SharePoint Databases individually in SQL Server:
- SELECT DB_NAME(database_id) AS DatabaseName,
- Name AS Logical_Name,
- Physical_Name, (size*8)/1024 SizeMB
- FROM sys.master_files
- To get the total number of SharePoint databases in the SQL server:
- select * from sys.databases
- or
- select COUNT(*) from sys.databases
- To find the path for SQL Server error logs:
- o sp_readerrorlog
- To get the total number of site collections in a Web application :Note: Point to the content database hosting that site collection and run this query
- select count(*) as 'Total Site Collection' from sites
- To get the total number of sites in a web application :Note: Point to the content database hosting that site collection and run this query
- select count(*) from Webs
- To get the Site Title and Site ID :
- select Title as 'Site title',FullUrl, SiteId as 'Site Collection Id' from Webs order by SiteId
- To get the number of sites under each site collection in a web application :Note: Point to the content database hosting that site collection and run this query.
- selec tSiteId, count(*) as 'Total Sub Sites' from Webs inner join Sites on Sites.Id = Webs.SiteId group by SiteId
Read more articles on SharePoint:


Mohammed IbrahimPosted Mar 14, 2016, 12:09 AM EDT
nice
Humayun Kabir MamunPosted Mar 12, 2016, 10:16 PM EST
Nice...
Prashant VermaPosted Mar 11, 2016, 9:36 PM EST
nice
Vignesh ManiTOP 100Posted Mar 11, 2016, 12:42 PM EST
nice
Debasis SahaTOP 100Posted Mar 11, 2016, 10:25 AM EST
good one
Kashif SohailPosted Mar 11, 2016, 9:43 AM EST
nice
kalu singh raoPosted Mar 11, 2016, 3:54 AM EST
Nice...
Sr KarthigaPosted Mar 11, 2016, 2:56 AM EST
nice one
Sr KarthigaPosted Mar 11, 2016, 2:56 AM EST
well explained
Yashwant VishwakarmaPosted Mar 11, 2016, 2:55 AM EST
Really Nice queries, thanks for sharing !!
Anu VPosted Mar 11, 2016, 12:58 AM EST
nice